Java Tutorial · Java Basics · Lesson 3 of 37

Java Syntax

Classes, the main method, statements, semicolons and braces.

Anatomy of a Java program

Example 1
public class Main {
    public static void main(String[] args) {
        int apples = 3;
        int oranges = 4;
        int total = apples + oranges;
        System.out.println("Total fruit: " + total);
    }
}

Output

Total fruit: 7

Run this on your computer — see “Get Started” for setup.

  • public class Main — all Java code lives inside classes. Class names use PascalCase.
  • public static void main(String[] args) — the method the JVM runs first.
  • Statements end with a semicolon ;.
  • Curly braces { } mark the start and end of classes, methods and blocks.

Case sensitivity

Java is case-sensitive: myVar and myvar are different, and System must start with a capital S. Conventions: camelCase for variables and methods, PascalCase for classes, UPPER_SNAKE_CASE for constants.

Test yourself

What must every Java statement end with?

  1. .
  2. ;
  3. :
  4. A new line
Show answer

Answer: ;