Your first Java program
What you'll learn:
- write a program from a
Mainclass with amainmethod and run it withjava Main.java; - print text with
System.out.printlnandSystem.out.printand predict where a new line starts; - explain what
javacand the JVM do and why one program runs on any computer; - read the compiler's messages and fix the errors it finds.
A program is a list of instructions for a computer. Picture sheet music for an orchestra, the score. It lists what to play in order, and the orchestra plays it bar by bar, from the first to the last. A computer runs a program the same way: instruction after instruction, top to bottom.
Music is written in special signs that every musician understands. A program is also written in a special language, and in this course that language is Java. Here is a program that prints one line:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java"); // Hello, Java
}
}Let's walk through it line by line:
public class Main {opens a class namedMain, a named block that holds code. In Java, every instruction lives inside some class.public static void main(String[] args) {opens the methodmain, a named set of instructions inside a class. Every program in this course writes this line the same way, word for word.System.out.println("Hello, Java");is the only instruction: printHello, Java.- The first
}closes the methodmain. - The second
}closes the classMain.
Text from // to the end of the line is a comment, and Java skips it. In the course examples, a comment at the end of a line shows what that line prints.
Where execution starts
When the program starts, Java finds the main method in the class and runs its body: the instructions between the curly braces after main(String[] args). That body is the score the computer plays:
- instructions run one after another, top to bottom;
- after the last instruction, the program ends.
Braces and semicolons
Java text has a strict shape. Three rules hold it together:
- curly braces
{and}come in pairs: the opening one starts a block, and the closing one ends it; - the pair of braces of the method
mainsits inside the pair of braces of the classMain, like a box inside a box; - every instruction ends with a semicolon, the way a sentence ends with a period.
Java does not read indentation, but it shows a person what sits inside what. In this course, each level of nesting is indented by four spaces.
Printing: println and print
System.out is the program's output: the place where printed text appears. Read System.out.println(…) as: print to the output whatever is in the parentheses. Text in double quotes is printed as is, without the quotes. A number can be written without quotes.
Printing comes in two variants. The difference between them is the line break.
println: with a line break
System.out.println("Hello"); // Hello
System.out.println(42); // 42println prints a value and ends the line, so the next print starts on a new line. The ending ln helps you remember the difference: it is short for "line".
print: no line break
System.out.print("Ja");
System.out.print("va");
System.out.println("!"); // Java!print prints a value and stays on the same line. Let's see what each instruction does:
print("Ja")starts the line: it holdsJa.print("va")addsvaright after it.printitself adds no spaces.println("!")adds!and ends the line. The result isJava!.
In the widget, these lines sit inside a whole program. Press "Next step" and watch which step finishes each line of output. Next to the code, the widget shows the method running now: Main.main, that is, the method main of the class Main. A line appears in the "output" box once println finishes it.
1public class Main {2 public static void main(String[] args) {3 System.out.println("Hello");4 System.out.print("Ja");5 System.out.print("va");6 System.out.println("!");7 System.out.println("Bye");8 }9}
Java calls the method main and starts with the first instruction of its body.
The order of lines in the output always matches the order of the instructions: Java neither reorders nor skips them.
Running the program
To run the program, take three steps:
- Save the text to a file named
Main.java. The file name matches the class name. - Open a terminal, a window for text commands, in the folder with this file.
- Type the command
java Main.java.
Before the program runs, the compiler checks the text: a program that enforces Java's rules and translates the text into a form that can run. It is a strict proofreader: if even one rule is broken, it shows an error, and the program does not start.
This is where the orchestra picture falls short. A musician can guess which note hides under an ink blot and play the piece to the end. The compiler does not guess: because of one error, no line runs, not even a correct one.
Common mistake
The closing brace of the class is missing:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
} // ✗error: reached end of file while parsingThe compiler read the file to the end and found no partner for the { on the first line: the class Main was left open. Add } as the last line. To see the pairs, put each closing brace right under the start of the line with its opening brace.
Key points
- A Java program is a
Mainclass with amainmethod; execution starts atmainand runs top to bottom. - Curly braces come in pairs, and every instruction ends with a semicolon.
printlnprints a value and ends the line, whileprintstays on the same line and adds no spaces.java Main.javachecks the program and runs it; if the compiler finds an error, not a single line runs.