Skip to content

Your first Java program

Let’s create the classic “Hello, World!” program.

Create a file named HelloWorld.java with the following content:

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

Explanation:

  • public class HelloWorld – declares a class named HelloWorld. The filename must match the class name (case‑sensitive).
  • public static void main(String[] args) – the entry point of any Java application. The JVM calls this method when the program starts.
  • System.out.println(...) – prints the argument to the standard output (console).

Open a terminal in the same directory and run:

Terminal window
javac HelloWorld.java

This produces HelloWorld.class – the bytecode file.

Execute the bytecode using the java launcher:

Terminal window
java HelloWorld

Output:

Terminal window
Hello, World!

Note: you do not include the .class extension when running.

Try modifying the message, adding multiple print statements, or even using variables.