Sub topic 3
Your First Program: Hello, World!
In this chapter you will write, compile, and run your very first Java program. You will learn what "compiling" means, why Java uses an extra step that many other languages skip, and how every piece of your tiny program works. By the end, you will have run real Java code on your own machine and understood every line of it.
- What you need before starting: access to a command line or terminal, and Java installed (a JDK, version 11 or later). If you type
java --versionin a terminal and see a version number, you are ready. - What you will build: a program that prints the text
Hello, World!to the screen.
Why Hello, World!?
Almost every programmer's first program prints the words "Hello, World!". It is a tradition that goes back to the 1970s. The task sounds trivial, but it quietly proves that your whole toolchain works: the compiler is installed, the runtime is installed, your file is in the right place, and your code is syntactically correct. If Hello, World! runs, everything else in this tutorial will run too.
What Happens When You Run Java Code
Java does not run your source code directly. There are two steps:
- Compilation: a program called the Java compiler (
javac) reads your human-readable source file and translates it into bytecode, a compact instruction format stored in a.classfile. - Execution: the Java Virtual Machine (JVM) is a program that reads bytecode and executes it, instruction by instruction, on your actual computer.
A compiler is a translator that converts code written for humans into a form a machine can process. The JVM is a virtual machine: a piece of software that imitates a computer and runs your bytecode on it.
This two-step design is the reason Java is called platform independent — "write once, run anywhere." The same .class file can run on Windows, macOS, or Linux, because each platform has its own JVM that understands the same bytecode.
flowchart LR
A["Hello.java\n(source code)"] -->|"javac compiles"| B["Hello.class\n(bytecode)"]
B -->|"java runs"| C["JVM"]
C --> D["Output:\nHello, World!"]
B --> E["Windows JVM"]
B --> F["macOS JVM"]
B --> G["Linux JVM"]caption: The Java pipeline from source code to output on different platforms. alt: A flow diagram showing Hello.java being compiled by javac into Hello.class bytecode, which is then run by the JVM to produce the output Hello, World, with the same bytecode also feeding Windows, macOS, and Linux JVMs.
Writing the Program
Create a folder for your work. Inside it, create a file named exactly HelloWorld.java. The file name matters: in Java, a public class must live in a file with the same name, with a capital letter match and everything.
Open the file and type this exactly:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Save it. That is a complete, runnable Java program. Now let us take it apart, line by line.
Anatomy of the Program
The class declaration
public class HelloWorld {- A class is a container for code. Every line of Java code lives inside some class; there is no way around this.
HelloWorldis the name you chose. By convention, class names start with a capital letter and use CamelCase, joining words together with capital letters instead of spaces.publicmeans this class is visible to any other code that wants to use it. For a small program, it simply signals "this is the main class."- The
{brace opens the class body. Everything down to the matching}belongs to the class.
The main method
public static void main(String[] args) {This is the entry point — the exact place the JVM starts running your program. Every word here has a meaning:
| Part | Meaning |
|---|---|
| `public` | Any code can call this method; the JVM must be able to reach it. |
| `static` | The method belongs to the class itself, not to any object. The JVM can start it without creating anything first. |
| `void` | The method returns no value back to the caller. |
| `main` | The fixed name the JVM looks for. Spelling it `Main` or `main2` means the JVM cannot find a starting point. |
| `String[] args` | A parameter holding any command-line arguments typed after the program name, as an array of text values. |
You do not need to fully absorb public static void yet; these are explained in later chapters. For now, treat the line as a fixed opening you write at the start of every program. What matters is the shape:
flowchart TD
A["file: HelloWorld.java"] --> B["class HelloWorld"]
B --> C["method main"]
C --> D["statement:\nSystem.out.println"]caption: The nesting structure of a Java program: file contains class, class contains method, method contains statements. alt: A top-down flowchart showing the file HelloWorld.java containing the class HelloWorld, which contains the main method, which contains the statement System.out.println.
The print statement
System.out.println("Hello, World!");System.outis Java's built-in connection to your terminal's standard output — the normal place text appears.printlnis short for "print line." It prints its argument, then moves to a new line."Hello, World!"is a string literal: text in double quotes that the program treats as data.- The
;semicolon ends the statement. Every statement in Java ends with a semicolon. Forgetting it is the most common beginner error.
The closing braces
The two } characters close the method and then the class. Braces in Java always come in pairs, and each opens and closes a block of code.
Compiling and Running
Open a terminal, move into your folder, and run:
javac HelloWorld.javaIf nothing prints, that is good news — silence means success. You will now find a file named HelloWorld.class in the folder. That is your bytecode.
Now run it:
java HelloWorldThe terminal prints:
Hello, World!If you get an error instead:
| Message you might see | Likely cause |
|---|---|
| `javac: command not found` or `'javac' is not recognized` | The JDK is not installed, or not on your system PATH. Reinstall the JDK. |
| `class HelloWorld is public, should be declared in a file named HelloWorld.java` | The file name does not match the class name exactly, including capitals. |
| `cannot find symbol` or `';' expected` | A typo in the code, or a missing semicolon. Check character by character. |
| `Could not find or load main class HelloWorld` | You tried to run before compiling, or you are in the wrong folder. |
Experimenting: Break It on Purpose
The fastest way to learn what each piece does is to remove it and read the error. Try each of these (undo each change before the next):
- Misspell
mainasMain, then run. The JVM says it cannot find the main method. This provesmainis a reserved entry point name. - Remove a semicolon from the println line and recompile. The compiler refuses to build and points at the line. Compilation errors stop the program before it ever runs.
- Print a second line. Add another statement below the first:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("This is my second line.");
}
}Statements run top to bottom, in order. Recompile and rerun to see both lines.
- Print something of your own. Change the text between the quotes. Text inside double quotes is data; you can put anything there. But a missing quote mark is a compile error, and a smart quote (the curly “ ” character some editors insert) is also an error — Java only accepts the straight
".
Command-Line Arguments
Remember String[] args? If you run:
java HelloWorld Adathen inside your program, args contains the text "Ada". You can print it:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, " + args[0] + "!");
}
}The + sign joins text together. args[0] means "the first item in the args list." Running java HelloWorld Ada prints Hello, Ada!. Running it with no arguments crashes with an error, because args[0] does not exist — a safe, early lesson: Java fails fast when you ask for something that is not there.
What You Learned
- Java source code (
.java) is compiled byjavacinto bytecode (.class), which the JVM executes. - This makes Java platform independent: one bytecode file runs on any platform with a JVM.
- Every Java program has at least one class; execution starts at the
mainmethod. System.out.println(...)prints text to the terminal.- Statements end with semicolons; code is grouped with braces; the public class name must match the file name.
- Compilation errors (typos, missing semicolons) stop the program before it runs; runtime errors (like a missing argument) happen while it runs.
Exercise
You run java Hello and get "Could not find or load main class Hello". What are two likely causes?
Show answer
You never compiled, so there is no Hello.class file — run javac Hello.java first. Or you are in a different folder than the one containing Hello.class.
Exercise
Why must a public class be declared in a file named exactly after it?
Show answer
The compiler and JVM locate code by file name. The fixed naming rule lets javac and java find the right class files without searching, and keeps projects organized predictably.
Exercise
What does static mean in public static void main?
Show answer
It means the method belongs to the class itself rather than to any object, so the JVM can call it immediately without first creating an object.