Java Documentation

1.1 Your First Java Program

A step-by-step walkthrough of the classic Hello World example, compilation and execution commands, and a gentle explanation of each part of the program.

Hello World in Java

Below is a minimal but complete Java program. Type it exactly as shown in your editor and save it as HelloWorld.java.

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

Compiling and Running the Program

Use the terminal or command prompt to navigate to the folder whereHelloWorld.java is saved, then run the following commands:

Compile & Run
javac HelloWorld.java
java HelloWorld

If everything is typed correctly and Java is installed properly, your output will look like this:

Hello, World!

Understanding the Program Line by Line

  • public class HelloWorld: defines a public class named HelloWorld. The file name must match this class name.
  • public static void main(String[] args): entry point of every standalone Java program. The JVM starts execution from this method.
  • System.out.println("Hello, World!");: prints a line of text to the console followed by a newline.
  • Curly braces { and } group related statements into blocks (class body and method body).
  • Each statement ends with a semicolon ;, which tells the compiler where one instruction finishes.

Frequently Asked Questions

These are typical conceptual questions students ask when seeing their first Java program. Each question will be discussed in more detail in later sections.