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
HelloWorldmust live in a file namedHelloWorld.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 / token | What it means |
|---|---|
public | Visible to the JVM from anywhere |
static | Belongs to the class itself, not to an instance |
void | Returns nothing |
main | Special name the JVM looks for |
String[] args | Array of command-line arguments passed in at runtime |
Tip: Since Java 21 you can write
void main()(nopublic 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 injava.langthat gives you access to standard I/O.out— a static field of typePrintStreamrepresenting 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
| Method | Newline appended? | Supports format specifiers? |
|---|---|---|
println | Yes | No |
print | No | No |
printf | No (use %n) | Yes |
Common Errors and Fixes
| Error message | Likely cause | Fix |
|---|---|---|
error: class HelloWorld is public, should be declared in a file named HelloWorld.java | File name ≠ class name | Rename the file or the class |
error: reached end of file while parsing | Missing closing } | Count your braces |
Main method not found in class … | Wrong method signature | Use public static void main(String[] args) exactly |
cannot find symbol | Typo in a class or method name | Check 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:
- Class loading — the bootstrap ClassLoader finds and reads
HelloWorld.classfrom the classpath (see Class Loaders). - Bytecode verification — the verifier confirms the bytecode is structurally sound and type-safe, a key security layer.
- 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). - Execution — the CPU runs the native code.
System.out.printlnultimately 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.
Related Topics
- How a Java Program Runs — follow the bytecode from
.javafile to CPU execution in detail. - JDK, JRE & JVM — understand the tools behind
javacandjava. - Setting the PATH — get your terminal ready to compile and run Java programs.
- JIT Compilation & Bytecode — see how the JVM turns bytecode into fast native code at runtime.
- Variables — the natural next step after printing: storing and using data.
- Command-Line Arguments — learn how to put that
String[] argsparameter to work.