TExcelTAD
Home › Java › Java Syntax

Java Syntax

Java programs follow a strict structure. Once you understand the handful of pieces in a minimal program, every other example in this course will make sense.

Anatomy of a program

java · try it yourself
class Main {
    public static void main(String[] args) {
        System.out.println("Learning Java syntax");
    }
}
Learning Java syntax

Let's read it piece by piece:

Code Meaning
class Main Declares a class named Main. In Java, all code lives inside a class.
{ ... } Curly braces mark the start and end of a block (class body, method body, ...).
public The method can be called from anywhere — the JVM needs this to start it.
static The method belongs to the class itself, so no object has to be created first.
void The method returns no value.
main The special name the JVM looks for as the entry point.
String[] args An array of command-line arguments (often unused).
System.out.println(...) Prints a value and moves to the next line.
; Every statement ends with a semicolon.

Note: In your own projects you will write public class Main in a file named Main.java. Our online compiler saves the code under another file name, so playgrounds leave out public before class. The public before static void main must stay.

Rules to remember

  • Java is case-sensitive. Main, main and MAIN are three different names. system.out.println will not compile.
  • Every statement ends with ;. Forgetting one is the most common beginner error.
  • Braces must match. Each { needs a closing }.
  • Class names start with a capital letter by convention (Main, BankAccount), while methods and variables use camelCase (printReport, totalPrice).
  • Whitespace mostly doesn't matter. Indentation is for humans; the compiler only cares about tokens. Still, consistent 4-space indentation makes code readable.

Statements run in order

Inside a method, statements execute from top to bottom:

java · try it yourself
class Main {
    public static void main(String[] args) {
        System.out.println("Step 1: boil water");
        System.out.println("Step 2: add pasta");
        System.out.println("Step 3: wait 10 minutes");
    }
}
Step 1: boil water
Step 2: add pasta
Step 3: wait 10 minutes

What a syntax error looks like

If you break a rule the compiler refuses to build the program and tells you where the problem is. This code forgets a semicolon:

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

Read compiler messages carefully — they name the line and usually describe the fix.

More than one class in a file

A file may contain several classes, but at most one of them can be public. In playgrounds we often put small helper classes next to Main:

java · try it yourself
class Greeter {
    static void greet(String name) {
        System.out.println("Hi, " + name + "!");
    }
}

class Main {
    public static void main(String[] args) {
        Greeter.greet("Ada");
        Greeter.greet("Linus");
    }
}
Hi, Ada!
Hi, Linus!

You will learn all about classes and methods later. For now, remember the skeleton: a class, a main method, and statements ending in semicolons.

Test yourself

What ends every Java statement?

  1. A period .
  2. A colon :
  3. A semicolon ;
  4. A new line

Which line will NOT compile?

  1. System.out.println("Hi");
  2. system.out.println("Hi");
  3. System.out.println(42);
  4. System.out.println("");

What does void in public static void main mean?

  1. The method is empty
  2. The method returns nothing
  3. The method is private
  4. The method cannot be called