Skip to content
Java getting started 5 min read

Your First Java Program

Every Java journey starts here. In this page you will write a tiny program that prints a message to the screen, compile it into bytecode, and run it on the JVM — all while understanding exactly what each piece of code does.

The Complete Program

Create a file named HelloWorld.java and type (or paste) the following:

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

Output:

Hello, World!

That is the smallest complete Java program you can write. Only three things are mandatory: a class, a main method, and at least one statement inside it. Everything else is built on top of this skeleton.

Note: The file name must exactly match the class name, including capitalisation. A class named HelloWorld must live in a file named HelloWorld.java. If they differ, the compiler throws an error.


Anatomy of the Program

Let’s walk through every word on the page.

public class HelloWorld

Java organises all code inside classes. Think of a class as a blueprint or a container. The keyword public makes the class visible to the whole program (and to the Java launcher). HelloWorld is simply the name you chose.

You will learn much more about classes on the Classes & Objects page.

public static void main(String[] args)

This is the entry point — the exact signature the JVM looks for when you run a program. Break it down:

Keyword / tokenWhat it means
publicVisible to the JVM from anywhere
staticBelongs to the class itself, not to an instance
voidReturns nothing
mainSpecial name the JVM looks for
String[] argsArray of command-line arguments passed in at runtime

Tip: Since Java 21 you can write void main() (no public static, no args) when using unnamed classes — a preview feature great for quick experiments. For production code, stick with the full signature.

System.out.println("Hello, World!")

This single statement does the printing.

  • System — a built-in class in java.lang that gives you access to standard I/O.
  • out — a static field of type PrintStream representing the standard output stream.
  • println(...) — prints the argument followed by a newline.

The string "Hello, World!" is a string literal — any sequence of characters wrapped in double quotes.


Compiling and Running

You need the JDK installed and your PATH set before these commands work.

Step 1 — Compile

Open a terminal, navigate to the folder containing HelloWorld.java, and run:

javac HelloWorld.java

javac is the Java compiler. It reads your .java source file and produces a .class file containing bytecode — a compact, platform-neutral instruction set understood by the JVM.

Step 2 — Run

java HelloWorld

java launches the JVM, which loads HelloWorld.class, finds the main method, and executes it. Notice you omit the .class extension.

Output:

Hello, World!

Warning: A very common beginner mistake is typing java HelloWorld.class. The launcher expects a class name, not a file name. Drop the .class.

One-File Programs (Java 11+)

Since Java 11 you can skip the explicit compile step for single-file programs:

java HelloWorld.java

Java compiles and runs in one shot. This is handy for quick scripts but is not used in real projects.


Printing Variations

Once you have the skeleton working, try these alternatives inside main:

public class PrintDemo {
    public static void main(String[] args) {
        System.out.println("println adds a newline");
        System.out.print("print does not");
        System.out.print(" — still the same line\n");
        System.out.printf("Formatted: %d + %d = %d%n", 2, 3, 2 + 3);
    }
}

Output:

println adds a newline
print does not — still the same line
Formatted: 2 + 3 = 5
MethodNewline appended?Supports format specifiers?
printlnYesNo
printNoNo
printfNo (use %n)Yes

Common Errors and Fixes

Error messageLikely causeFix
error: class HelloWorld is public, should be declared in a file named HelloWorld.javaFile name ≠ class nameRename the file or the class
error: reached end of file while parsingMissing closing }Count your braces
Main method not found in class …Wrong method signatureUse public static void main(String[] args) exactly
cannot find symbolTypo in a class or method nameCheck capitalisation (System, not system)

Under the Hood

When you run javac HelloWorld.java, the compiler goes through several phases: lexical analysis, parsing into an Abstract Syntax Tree (AST), semantic analysis (type checking), and finally code generation. The output .class file is bytecode — a stack-based instruction set defined by the JVM specification, not native CPU instructions.

When you run java HelloWorld, the JVM:

  1. Class loading — the bootstrap ClassLoader finds and reads HelloWorld.class from the classpath (see Class Loaders).
  2. Bytecode verification — the verifier confirms the bytecode is structurally sound and type-safe, a key security layer.
  3. Interpretation / JIT — initially the JVM interprets bytecode. Once main (or any method) is called often enough, the JIT compiler translates it to optimised native machine code for your CPU (see JIT Compilation).
  4. Execution — the CPU runs the native code. System.out.println ultimately calls native I/O routines to write bytes to the OS standard output file descriptor.

This compile-once-run-anywhere model is what makes Java portable. The same HelloWorld.class runs unchanged on Windows, macOS, and Linux as long as a compatible JVM is installed.

Note: The entry-point resolution itself is simple: the JVM calls Class.getMethod("main", String[].class) reflectively at startup and invokes it. That is why the signature must be exact.


Last updated June 13, 2026
Was this helpful?