Skip to content
Theory · First program · lesson 1 of 8

Your first Java program

What you'll learn

What you'll learn:

  • write a program from a Main class with a main method and run it with java Main.java;
  • print text with System.out.println and System.out.print and predict where a new line starts;
  • explain what javac and 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:

JAVA
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java"); // Hello, Java
    }
}

Let's walk through it line by line:

  1. public class Main { opens a class named Main, a named block that holds code. In Java, every instruction lives inside some class.
  2. public static void main(String[] args) { opens the method main, a named set of instructions inside a class. Every program in this course writes this line the same way, word for word.
  3. System.out.println("Hello, Java"); is the only instruction: print Hello, Java.
  4. The first } closes the method main.
  5. The second } closes the class Main.

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 main sits inside the pair of braces of the class Main, 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

JAVA
System.out.println("Hello"); // Hello
System.out.println(42);      // 42

println 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".

JAVA
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:

  1. print("Ja") starts the line: it holds Ja.
  2. print("va") adds va right after it. print itself adds no spaces.
  3. println("!") adds ! and ends the line. The result is Java!.

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.

Liveprint and println step by stepstep 1 of 7 · frames: 1

Java calls the method main and starts with the first instruction of its body.

Watch which step finishes each line of output

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:

  1. Save the text to a file named Main.java. The file name matches the class name.
  2. Open a terminal, a window for text commands, in the folder with this file.
  3. 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:

JAVA
public class Main {
    public static void main(String[] args) {
        System.out.println("Hello, Java");
    } // ✗
TEXT
error: reached end of file while parsing

The 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 Main class with a main method; execution starts at main and runs top to bottom.
  • Curly braces come in pairs, and every instruction ends with a semicolon.
  • println prints a value and ends the line, while print stays on the same line and adds no spaces.
  • java Main.java checks the program and runs it; if the compiler finds an error, not a single line runs.