Lesson 3 — Language Rules

Java Syntax

Java's syntax is strict and explicit. The compiler enforces every rule at compile time — before your program ever runs. Understanding these rules deeply prevents the vast majority of beginner errors.

Java is Case-Sensitive — Always

Java treats uppercase and lowercase letters as completely different characters. String, string, and STRING are three different identifiers. This is not optional. The standard library class for text is String with a capital S — writing string will cause a compile error.

Every Statement Ends with a Semicolon

A statement is a complete instruction — declaring a variable, calling a method, assigning a value. Every statement must end with ;. Blocks (code inside { }) do not end with a semicolon.

JAVA — Semicolons
// Correct:
int age = 22;
System.out.println(age);

// Wrong — missing semicolons (compile errors):
int age = 22
System.out.println(age)

// Correct — no semicolon after the block:
if (age > 18) {
    System.out.println("Adult");
}

Blocks Use Curly Braces

Every class, method, if-statement, loop, and try-block is surrounded by { }. The opening brace can go on the same line as the declaration (preferred in Java) or on the next line. Be consistent — pick one style and stick to it.

JAVA — Nested blocks
public class MyClass {           // class block opens

    public static void main(String[] args) {   // method block opens
        if (true) {                            // if block opens
            System.out.println("inside if");
        }                                      // if block closes
    }                                          // method block closes

}                                              // class block closes

Naming Conventions

Java has standard naming conventions followed by every professional developer. The compiler does not enforce them — but your colleagues and employers will:

  • Classes — PascalCase: LearnerRecord, BankAccount, CustomerService.
  • Methods and variables — camelCase: calculateTotal, firstName, isEnrolled.
  • Constants — UPPER_SNAKE_CASE: MAX_RETRIES, VAT_RATE.
  • Packages — all lowercase, dot-separated: com.your-it-tutor.learner.

Whitespace and Indentation

Java ignores extra whitespace — the compiler does not care whether you indent with 2 spaces, 4 spaces, or tabs. But readable indentation is professional discipline. The standard is 4 spaces per level. Every block of code inside braces is indented one level further than the surrounding code.

JAVA — Indentation matters
// Hard to read — technically valid:
public class Bad{public static void main(String[] a){System.out.println("messy");}}

// Professional — same code:
public class Good {
    public static void main(String[] args) {
        System.out.println("clean");
    }
}

Complete Program Structure

Let's look at a slightly richer program that uses everything above — multiple statements, variables, a method call, and comments:

JAVA — Full program example
/**
 * A simple Your IT Tutor learner greeting program.
 * Demonstrates correct Java structure and syntax.
 */
public class LearnerGreeting {

    public static void main(String[] args) {
        // Declare variables
        String name    = "Thandi Mokoena";
        String city    = "Johannesburg";
        String course  = "Java";
        int    year    = 2025;

        // Print a multi-line greeting
        System.out.println("--- Your IT Tutor Learner Profile ---");
        System.out.println("Name:   " + name);
        System.out.println("City:   " + city);
        System.out.println("Course: " + course);
        System.out.println("Year:   " + year);
        System.out.println("------------------------------");
    }

}

Practice Task

Your Turn

Write a program MyProfile.java that prints your name, city, course, the current year, and a one-line career goal. Use proper indentation, consistent casing, and at least two comments. Compile and run it.

Common Mistakes

  • Missing semicolons at the end of statements.
  • Public instead of public — case matters everywhere.
  • Mismatched braces — every { must have a matching }. Count them.
  • Writing executable code directly inside the class but outside any method.
  • File saved as myprofile.java when the class is MyProfile — names must match exactly.

Professional Tip

Consistent style is not vanity — it is professionalism. Code is read by people far more often than it is written. Clear naming, proper indentation, and helpful comments make the difference between code that is maintained and code that is rewritten.

Mini Quiz

Which naming convention is correct for a Java method?