Java Tutorial
Java Syntax
Statements end with a semicolon. Blocks use braces. The compiler cares about types, not indentation.
Statements and semicolons
Each statement ends with ;. Forget one and javac stops, often on the next line. Indentation is for people. Braces decide which lines belong together.
Example
public class Main {
public static void main(String[] args) {
int n = 4;
System.out.println(n);
}
}Blocks
A block is a pair of braces. main is a block. if and for use blocks too. The compiler does not care how many spaces you use, but a consistent two-space indent keeps examples readable.
Example
public class Main {
public static void main(String[] args) {
int score = 18;
if (score >= 10) {
System.out.println("pass");
System.out.println("score was " + score);
}
}
}Both print lines sit inside the if braces, so both run only when the condition is true. Without the braces, only the first line would belong to the if.
Case matters
Main and main are different names. String is the type. string is not. Keywords such as class and public are lowercase.
The method must be named main, not Main. The class can be Main.
Area of a rectangle
Each step is one statement. Without the semicolons javac stops.
Example
public class Main {
public static void main(String[] args) {
int length = 4;
int width = 3;
int area = length * width;
System.out.println(area);
}
}A missing semicolon is the most common first error. javac often reports it on the next line, because that is where it first notices something is wrong — so when the error points at a line that looks fine, check the line above it.
Try It Yourself
Exercise: This program has two mistakes that stop it compiling. Find them.
public class Main {
public static void Main(String[] args) {
int total = 5 + 3
System.out.println(total);
}
}Show solution
The method is spelled Main with a capital M — it must be lowercase main, or the program has no entry point. And the int total = 5 + 3 line is missing its semicolon.
Key Takeaways
- Every statement ends with a semicolon.
- Braces group lines into blocks; indentation is for humans, not the compiler.
- Java is case-sensitive —
mainandMainare different names. - A semicolon error is often reported one line below where it actually is.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Maths
Area of a rectangle
Area is length times width. For a rectangle 4 m by 3 m that is 12 m². Each step is one statement. The semicolon is not decoration; without it javac stops.
A = l × w
Example
public class Main {
public static void main(String[] args) {
int length = 4;
int width = 3;
int area = length * width;
System.out.println(area);
}
}