Syntax & First Program

Now that Java is installed, let's take a closer look at the Hello World program from earlier and break down exactly what every piece means. Understanding this structure now will make everything that follows much easier.

Anatomy of a Java File

Every Java program starts with a class declaration. The file name must exactly match the public class name, including capitalization.

public class HelloWorld {
  public static void main(String[] args) {
    System.out.println("Hello, World!");
  }
}

Piece by piece:

  • public — an access modifier meaning this class is visible from anywhere.
  • class HelloWorld — declares a class named HelloWorld. This must be saved as HelloWorld.java.
  • public static void main(String[] args) — the method the JVM looks for and calls first when your program starts.
    • static — means main belongs to the class itself, so the JVM can call it without creating an object first.
    • void — main doesn't return any value.
    • String[] args — an array of command-line arguments passed in when the program is run.
  • System.out.println(...) — prints text followed by a new line to the console.

Case Sensitivity

Java is case-sensitive. This means HelloWorld, helloworld, and HELLOWORLD are three completely different identifiers. main must be written exactly in lowercase — Main or MAIN will not be recognized as the program's entry point.

Semicolons and Braces

Every statement in Java ends with a semicolon ;. Curly braces { } group statements into blocks — a class body, a method body, or the body of a loop or condition all use braces to mark where the block starts and ends.

if (isReady) {
  System.out.println("Go!");
}
Note: Forgetting a semicolon or a closing brace is one of the most common beginner mistakes — the compiler will point out the line, but the real cause is sometimes a line or two above it.

Comments

Comments are ignored by the compiler and exist purely to explain your code to humans. Java supports two styles:

// This is a single-line comment

/*
 * This is a multi-line comment.
 * It can span as many lines as you need.
 */
System.out.println("Comments don't affect output");

Basic Naming Conventions

Java doesn't force these rules on you, but following them makes your code readable to every other Java developer:

WhatConventionExample
Class namesPascalCase — each word capitalizedHelloWorld, BankAccount
Method & variable namescamelCase — first word lowercase, rest capitalizedmain, totalAmount, printReport
Next steps: Now that you understand the structure of a Java file, head to Variables to learn how to store and work with data.