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
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 Mainin a file namedMain.java. Our online compiler saves the code under another file name, so playgrounds leave outpublicbeforeclass. Thepublicbeforestatic void mainmust stay.
Rules to remember
- Java is case-sensitive.
Main,mainandMAINare three different names.system.out.printlnwill 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:
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:
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:
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?
- A period .
- A colon :
- A semicolon ;
- A new line
Which line will NOT compile?
- System.out.println("Hi");
- system.out.println("Hi");
- System.out.println(42);
- System.out.println("");
What does void in public static void main mean?
- The method is empty
- The method returns nothing
- The method is private
- The method cannot be called