← The curriculum
§ 1.01 · Foundations

Hello, World — your first Java program

Set up the JDK, understand the anatomy of a Java class, and run a program from the command line.

Beginner · 12 minute read
Try it yourself
Live Java · editable

Runs on a sandboxed JVM via the public Piston API. Edit the code and hit Run.

§ 1.01.01

Why start here

Every Java program begins as a class with a main method. Before we discuss types, control flow, or objects, we need a program that compiles and runs. This lesson establishes that loop: edit, compile, run — the same loop you will use for years to come.

§ 1.01.02

Installing the JDK

Download a long-term-support JDK — Java 21 is the current LTS. On macOS, brew install openjdk@21 works well; on Linux, use SDKMAN (sdk install java 21-tem); on Windows, the Adoptium installer sets PATH for you. Confirm with java -version and javac -version. Both commands must print the same major version, or your compiled classes will target the wrong runtime.

§ 1.01.03

Anatomy of the program

public class Hello declares a class named Hello, and the file must be Hello.java to match. public static void main(String[] args) is the entry point the JVM calls; the signature is fixed, but you can rename args. System.out.println prints a line to standard output — System.out is a PrintStream field on the System class.

Example
Live Java · editable

Runs on a sandboxed JVM via the public Piston API. Edit the code and hit Run.

§ 1.01.04

Compile and run

Save the file as Hello.java. Run javac Hello.java to produce Hello.class — bytecode targeting the JVM, not your CPU. Then java Hello (no .class suffix) executes it. From Java 11 onward, java Hello.java compiles and runs in a single step, which is perfect for scratch files and this course.

§ 1.01.05

What happens under the hood

The JVM loads Hello.class, verifies the bytecode, links referenced classes lazily, and hands control to main. System.out.println ultimately calls a native write into the OS's stdout file descriptor. Understanding this pipeline pays off later when you debug classpath errors, class loaders, or missing runtime dependencies.

§ 1.01.06

Common first-day errors

Error: Could not find or load main class almost always means you ran java Hello.class (drop the .class) or you're in the wrong directory. public class X in a file named Y.java fails with a filename mismatch — Java requires the public class name to match the file. A missing semicolon or brace produces a compile error with a line number; read it top-down and fix only the first one, because later errors are often cascades of the first.