← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 5 of 12

Java Syntax & Structure

Java has strict spelling and grammar rules, like a language with careful punctuation. Capital letters matter, curly braces group the steps, and every statement ends with a semicolon.
Syntax
java
public class ClassName {
    public static void main(String[] args) {
        statement;  // each statement ends with ;
    }
}

Case Sensitivity

Java treats uppercase and lowercase letters as entirely different characters, so myVariable and MyVariable are two distinct identifiers -- this trips up beginners coming from case-insensitive languages.

Example: Case Sensitivity

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Declare myVariable and set it to 1
		int myVariable = 1;
		// Declare MyVariable and set it to 2
		int MyVariable = 2;
		// Print a line to the console
		System.out.println(myVariable + " and " + MyVariable + " are different variables.");
	}
}

Curly Braces

Every class body, method body, loop, and conditional block is delimited by a matching pair of curly braces, and a missing or misplaced brace is one of the most common compiler errors you'll encounter.

Example: Curly Braces

java
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Check whether true
		if (true) {
			// Print a line to the console
			System.out.println("Inside the if block");
		}
	}
}

Class Naming Conventions

Convention (enforced by tooling, not the compiler) says class names use PascalCase, capitalizing the first letter of each word, like BankAccount or HttpClient -- following it keeps your code readable to other Java developers.

Example: Class Naming Conventions

java
// Define the class BankAccount
class BankAccount {
	double balance;
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new BankAccount object and store it in account
		BankAccount account = new BankAccount();
		// Print a line to the console
		System.out.println("Class named in PascalCase: BankAccount");
	}
}

Method Naming Conventions

Method names follow camelCase instead, starting lowercase, like calculateTotal() -- this visual distinction from PascalCase class names helps you tell at a glance whether an identifier is a type or a behavior.

Example: Method Naming Conventions

java
// Define the class Main
public class Main {
	static int calculateTotal(int a, int b) {
		// Send a + b back to the caller
		return a + b;
	}
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println("Method named in camelCase: " + calculateTotal(2, 3));
	}
}

Indentation and Whitespace

The compiler doesn't care how you indent your code, but consistent indentation (typically 4 spaces per nesting level) is what makes deeply nested loops and conditionals actually readable to a human.

Example: Indentation and Whitespace

java
// Define the class Main
public class Main {
    // Program entry point: the JVM starts running here
    public static void main(String[] args) {
        // Loop: repeat while the condition holds
        for (int i = 0; i < 2; i++) {
            // Print a line to the console
            System.out.println("Consistently indented nested block: " + i);
        }
    }
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using the wrong case, such as string instead of String, since Java is case-sensitive.
  2. Forgetting to close a curly brace, so the compiler reports an error at the end of the file rather than where the brace is missing.
  3. Leaving out the semicolon at the end of a statement, which gives ';' expected.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.