W3docs

Java Hello World Program

Write, compile, and run your first Java program — a classic Hello World — and understand each part of the source file.

Every programming-language tutorial starts with the same ritual: a tiny program that prints Hello, World!. Java's version of it has more boilerplate than most languages, but every part of that boilerplate is something you'll see again. This chapter unpacks the program line by line.

You can edit the example below and press Run — it actually compiles and executes on the server:

java— editable, runs on the server

The full program

Here is the complete source file. By convention, it would be saved as HelloWorld.java — Java requires the file name to match the public class name.

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

Walking through it line by line

public class HelloWorld

Every Java program lives inside a class. A class is a blueprint that bundles data and behavior together — the cornerstone of object-oriented programming. Here the class is called HelloWorld, and the public keyword means it's visible from any other class.

The convention is that class names use UpperCamelCase: HelloWorld, BankAccount, Order.

public static void main(String[] args)

This is the entry point of the program — the method the JVM calls when you launch your code. The signature is precise: change any part of it and the JVM won't find it.

  • public — the JVM needs to call it from outside the class.
  • static — it can be called without first creating an instance of HelloWorld.
  • void — it doesn't return a value.
  • main — the special name the JVM looks for.
  • String[] args — an array of command-line arguments passed in when the program starts. You can name it anything (args is conventional).

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

System is a class in the standard library; out is a static field on it that holds the standard output stream; println is a method on that stream that prints its argument followed by a newline.

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

Every Java statement ends with a semicolon (;). The compiler will complain if you forget one.

Compile and run from the terminal

If you have the JDK on your PATH (see the previous two chapters), you can compile and run from any terminal:

# Save the source as HelloWorld.java, then:
javac HelloWorld.java       # compiles to HelloWorld.class
java HelloWorld             # runs the bytecode

Output:

Hello, World!

Notice that you pass HelloWorld to java, not HelloWorld.class or HelloWorld.java. The java launcher takes the fully qualified class name and finds the matching .class file on the classpath.

Try a variation

Edit the runnable example to print something else — your name, the current year, a multi-line message — and press Run. Some ideas:

System.out.println("Hello from Java " + System.getProperty("java.version"));
System.out.println("Line 1");
System.out.println("Line 2");
System.out.printf("%s is %d years old%n", "Ada", 36);

The last two methods to know:

  • println — prints its argument and a newline.
  • print — like println but without the trailing newline.
  • printf — printf-style formatting, exactly like C's.

Common beginner mistakes

A few things that catch newcomers:

  • Filename mismatch. A public class HelloWorld must be in a file called HelloWorld.java. Otherwise javac errors out.
  • Missing semicolon. Every statement needs one. The compiler's error messages are usually clear about which line.
  • Println vs println. Java is case-sensitive.
  • Smart quotes. Copying code from a website that "helpfully" turned " into " will produce a baffling compiler error.

What's next

How to Compile and Run a Java Program goes deeper into what javac and the java launcher actually do, including the classpath, packages, and using jshell for quick experiments without a full program.

Practice

Practice

Which signature is the correct entry point for a Java program?