Back

Setting Up and Running Java

divider

What You Need

One thing: a code editor that can compile and run Java and show you what it prints. That is the entire requirement for all four units. Nothing in this course needs a graphics library, a database, or a package manager.

Use the code editor set up for this course. It runs in a browser tab, so it works the same on a Chromebook as on anything else, and there is nothing to install.

If you already have Java running some other way — a desktop editor, a JDK on your own machine — keep it. Every program in this course is a plain .java file with no dependencies, so it runs anywhere Java runs.

divider

Your First Program

Make a file called Main.java and put this in it. Type it rather than pasting it — there is a reason, and it is the next section.

Main.java
public class Main
{
public static void main(String[] args)
{
System.out.println("Ready.");
}
}

Run it. You should see:

Terminal window
Ready.

The file name has to match the class name. A class called Main lives in a file called Main.java, capital M and all. This is a rule Java actually enforces, and it is the first thing that catches people arriving from Python or JavaScript.

divider

The Three Kinds of Wrong

This is the section worth reading twice. If your last language was Python or JavaScript, Java fails at different times than you are used to, and the whole experience of writing it depends on recognizing which kind of failure you are looking at.

1. It will not compile: a syntax error

The program is not valid Java, so it never runs at all. Drop one semicolon:

Main.java
public class Main
{
public static void main(String[] args)
{
System.out.println("Ready.")
}
}
Terminal window
Main.java:5: error: ';' expected
System.out.println("Ready.")
^
1 error

Note what you get: a file, a line number, and a caret pointing at the character. Compiler errors look hostile and are in fact the most helpful message in the language. Read the first one, fix it, recompile. Do not read all forty — one mistake often produces a cascade, and the first line is usually the only real one.

2. It will not compile: a type error

This is the category that does not exist in a dynamically typed language, and it is the reason Java feels strict at first:

int score = "seven";
Terminal window
Main.java:5: error: incompatible types: String cannot be converted to int
int score = "seven";
^

In Python this line runs fine and blows up somewhere else, later, with a stack trace that points at the wrong place. In Java it is caught before the program ever starts. You will trade a slower start for far fewer mysteries at 11pm. That trade is most of what static typing buys you.

3. It compiled, then died: a run-time exception

The code was valid. It just asked for something impossible once it was running:

int[] scores = new int[3];
System.out.println(scores[3]);
Terminal window
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Index 3 out of bounds for length 3
at Main.main(Main.java:6)

An array of length 3 has indexes 0, 1, and 2. There is no index 3. Read the exception name first — ArrayIndexOutOfBoundsException says exactly what happened — then read the line number.

There is a fourth kind, and it is the worst: the program compiles, runs, finishes, and prints the wrong answer. Nothing tells you. That one is on you to catch, and it is what the debug challenges in this course are for.

divider

One Note on Style

Every code sample in this course puts an opening brace on its own line:

public class Main
{
public static void main(String[] args)
{
System.out.println("Ready.");
}
}

You will see Java written the other way, with the brace at the end of the previous line, and both are correct. Java does not care and neither does the exam. Pick one and be consistent; this course is consistent about this one so that the block structure is easy to see at a glance while you are still learning to read it.

divider

Next

If Ready. printed, you have everything you need for the whole course. Read The Exam, and How to Pace Yourself before starting Unit 1 — it is short, and it will change how you use the rest of these pages.