2. Hello, World!

Learning any programming language starts with the customary Hello, world! example. Modern Java lets you begin with a very small source file and then grow the program into the classic class-based form when you are ready.

2.1. Compact source file

For a first program on JDK 25 or newer, place this code in hello.java.

void main() {
  IO.println("Hello, world!");
}

Run it directly with the Java launcher.

java hello.java

This form is called a compact source file. It keeps the first example focused on statements, method calls, strings, and output. IO is a small console I/O class in java.lang, so it is available without an import.

2.2. Growing the program

The same program can grow into an explicit class while keeping an instance main method.

class HelloWorld {
  void main() {
    IO.println("Hello, world!");
  }
}

This is a useful bridge: students can see that methods live inside classes without learning every modifier on the first page.

2.3. Classic main method

Large Java programs still commonly use the classic static entry point shown below. The example project uses this form because Maven, packages, tests, and IDEs all handle it consistently.

1public class HelloWorld {
2
3  public static void main(String[] args) {
4    System.out.println("Hello, world!");
5  }
6
7}

The rest of the book uses complete class-based examples. When a chapter shows only the body of main, remember that the code still lives inside a class in the full source file.