☕ Java
First Java Program
Every Java developer started with the same program — Hello, World. But there's more happening in those five lines than most tutorials bother to explain. Here's how to write, compile, and run your first Java program, and actually understand every word of it.
What You Need Before You Start
Before writing a single line of Java, you need the JDK (Java Development Kit) installed on your machine. The JDK includes the compiler (javac) and the runtime (java) — both are required.
Check if Java is already installed by opening a terminal and running:
Shell
java -version
// Expected output (version may differ):
// java version "21.0.1" 2023-10-17
// Java(TM) SE Runtime Environment (build 21.0.1+12-29)
// Java HotSpot(TM) 64-Bit Server VM (build 21.0.1+12-29, mixed mode)
javac -version
// Expected output:
// javac 21.0.1The Program — All Five Lines
Here it is — the Hello World program every Java developer has written:
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Breaking It Down — Line by Line
Don't just copy-paste and move on. Every word in those five lines means something specific. Here's what each part actually does:
public class HelloWorld
- public — This class is accessible from anywhere
- class — You're defining a class. In Java, everything lives inside a class. No exceptions.
- HelloWorld — The name of your class. This must exactly match your filename: HelloWorld.java. Case-sensitive.
public static void main(String[] args)
This is the entry point — the method the JVM looks for and calls first when you run the program.
- public — The JVM needs to call this from outside the class, so it must be public
- static — The JVM calls main before creating any object, so it must be static (no instance needed)
- void — main doesn't return anything
- main — The exact name the JVM looks for. Not Main, not MAIN. Exactly main.
- String[] args — An array of command-line arguments passed to the program. Empty if you don't pass any.
System.out.println("Hello, World!")
- System — A built-in Java class that provides access to system resources
- out — A static field on System representing the standard output stream (your terminal)
- println — "print line" — prints the text and moves to a new line
- "Hello, World!" — A String literal — text wrapped in double quotes
Compiling and Running
Save your file as HelloWorld.java — the filename must match the class name exactly, including capitalization. Then open a terminal in the same directory and run:
Shell
# Step 1: Compile the source code
javac HelloWorld.java
# If successful, no output is shown. A new file appears:
# HelloWorld.class ← this is the bytecode
# Step 2: Run the compiled program
java HelloWorld
# Output:
Hello, World!
# Note: when running, you use the class name (HelloWorld)
# NOT the filename (HelloWorld.class) — no .class extensionCommon Errors on Your First Program
First-timers hit the same errors every time. Here's what they mean and how to fix them:
Java
// Error 1: Filename doesn't match class name
// File saved as helloworld.java, class named HelloWorld
// Fix: rename the file to HelloWorld.java
// Error 2: Missing semicolon
System.out.println("Hello, World!") // ← no semicolon
// javac error: ';' expected
// Fix: every statement in Java ends with a semicolon
// Error 3: Wrong quotes — using smart/curly quotes
System.out.println("Hello, World!"); // ← these are NOT straight quotes
// Fix: always use straight double quotes: "Hello, World!"
// Error 4: Running with .class extension
java HelloWorld.class
// Error: Could not find or load main class HelloWorld.class
// Fix: java HelloWorld (no extension)
// Error 5: Capitalization mismatch
public class helloworld { ... } // saved as HelloWorld.java
// javac error: class helloworld is public, should be declared in a file named helloworld.java
// Fix: class name and filename must match exactlyTaking It Further — Printing Different Things
Now that it works, experiment. Try printing different data types:
Java
public class HelloWorld {
public static void main(String[] args) {
// Print a string
System.out.println("Hello, World!");
// Print a number
System.out.println(42);
// Print a decimal
System.out.println(3.14);
// Print the result of an expression
System.out.println(10 + 20); // prints 30
// Print without a new line (print vs println)
System.out.print("Hello, ");
System.out.print("World!");
System.out.println(); // just a newline
// Print multiple values using concatenation
String name = "Alice";
int age = 25;
System.out.println("Name: " + name + ", Age: " + age);
}
}Using an IDE Instead of the Terminal
The terminal approach teaches you what's actually happening under the hood — which is exactly why you should do it at least once. But in real development, everyone uses an IDE.
The two most popular choices for Java:
IntelliJ IDEA (recommended)
- The industry standard for Java development
- Free Community Edition covers everything you need as a beginner
- Download from jetbrains.com/idea
- Create a new project → New Java class → type your code → press the green Run button
VS Code with the Java Extension Pack
- Lighter weight than IntelliJ, good if you're already using VS Code
- Install the "Extension Pack for Java" from Microsoft in the VS Code marketplace
- Works well for smaller projects and beginners
Both IDEs handle compilation automatically — you just write code and hit run. But understanding javac and java HelloWorld from the terminal means you're never confused about what the IDE is doing behind the scenes.
What Just Happened — The Full Picture
You just ran a Java program. Here's what actually happened in those two commands:
javac HelloWorld.java:
1. The compiler read your .java source file
2. It checked for syntax and type errors
3. It translated your code into platform-neutral bytecode
4. It wrote that bytecode to HelloWorld.class
java HelloWorld:
1. The JVM started up
2. The Class Loader found and loaded HelloWorld.class
3. The Bytecode Verifier confirmed it was safe to run
4. The Execution Engine found the main method and started executing
5. System.out.println sent "Hello, World!" to your terminal
6. main returned, the JVM shut down
Five lines of code. Two commands. Six stages of execution. That's Java.
Related Topics in Introduction
What is Java?
Java is a high-level, object-oriented programming language built on one killer idea: write your code once, and run it on any device — Windows, Mac, Linux, phone, smartwatch, you name it. No rewrites needed.
Features of Java
Java didn't become one of the world's most used languages by accident. From running on any device to handling millions of users simultaneously, here's what makes Java genuinely powerful — and why companies keep betting on it.
Uses of Java
Java powers everything from Android apps to banking systems, from Netflix's backend to NASA's mission control. Here's where Java is actually used in the real world — and why it keeps showing up in the most critical systems on the planet.
Java Editions (Java SE, EE, ME)
Java isn't one-size-fits-all. It comes in three distinct editions — each built for a different environment. Whether you're building a desktop app, a banking backend, or firmware for a SIM card, there's a Java edition designed exactly for that job.