Java ยท Chapter 4 of 42

Java Syntax

Java syntax defines rules for writing valid programs: statements end with semicolons, code blocks are wrapped in curly braces, and everything (variables, methods) lives inside classes.

Java is case-sensitive, so `total` and `Total` are different identifiers. Class names conventionally start with an uppercase letter, while variables and methods use camelCase.

Syntax
public class Main {
  statement1;
  statement2;
}

Statements and blocks

Each instruction ends with a semicolon. Related statements are grouped into a block using curly braces, such as the body of a method or if statement.

Naming conventions

Classes use PascalCase (MyClass), while variables and methods use camelCase (myVariable). Constants use UPPER_SNAKE_CASE.

Example 1 (java)
public class Main {
  public static void main(String[] args) {
    int age = 25;
    System.out.println("Age: " + age);
  }
}
Output
Age: 25

A block is enclosed in braces, and each statement ends with a semicolon.

Key points

  • Statements end with a semicolon.
  • Curly braces group statements into blocks.
  • Java is case-sensitive.
  • Class names use PascalCase; variables use camelCase.
๐Ÿ’ก Note: Following naming conventions makes your code instantly recognizable to other Java developers.

๐Ÿ“ Quick Quiz

1. What ends a Java statement?

2. What naming style is used for classes?

3. What symbol groups statements into a block?