Hello, World! in Java: A Short History and Your First Program
Java runs everything from Android apps to bank systems and Minecraft. It’s a little more formal than some languages, so even “Hello, World!” takes a few lines, but every one of them has a job. Here’s where Java came from, and how to write and run your first program.
A short history of Java
In 1991, a small team at Sun Microsystems led by James Gosling started the “Green Project” to build software for consumer devices like set-top boxes. Gosling designed a new language for it and named it Oak, after a tree outside his office. The name was already taken as a trademark, so it was renamed Java, reportedly after the coffee the team drank in large quantities.
The devices never took off, but the web did. Sun announced Java in 1995 and released Java 1.0 in January 1996, with the slogan “write once, run anywhere.” Instead of compiling to instructions for one kind of processor, Java compiles to bytecode that runs on the Java Virtual Machine (JVM). Any computer with a JVM can run the same program, whether it’s Windows, a Mac, Linux or something else.
Java quickly became a favorite for large business systems and, later, for Android apps. Sun released Java as open source (OpenJDK) between 2006 and 2007, and Oracle took over when it bought Sun in 2010. Since 2017 a new version has come out every six months, with a long-term support (LTS) release every two years for people who want stability.
The code
Save this in a file called HelloWorld.java. The file name must match the class name exactly, including the capital letters:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
What each part does:
public class HelloWorld: in Java, all code lives inside a class. This one is calledHelloWorld.public static void main(String[] args): the main method, where every Java program starts.String[] argsholds any extra words typed after the program name when running it.System.out.println(...): prints a line of text to the screen. Java strings use double quotes.- Every statement ends with a semicolon, and curly braces
{ }group code together.
Steps to run it
1. Install a JDK
To write Java programs you need a JDK (Java Development Kit), which includes the compiler. Free options include Eclipse Temurin from adoptium.net and Oracle’s JDK from oracle.com/java. Pick the latest LTS version for your operating system and run the installer.
Open a terminal (Terminal on Mac, PowerShell or Command Prompt on Windows) and check it worked:
java -version
javac -version
Both should print a version number.
2. Compile
Go to the folder containing HelloWorld.java and compile it:
cd path/to/your/folder
javac HelloWorld.java
This creates a new file, HelloWorld.class, containing the bytecode.
3. Run
Run the class by name (without .class on the end):
java HelloWorld
The terminal prints:
Hello, World!
A shortcut
Since Java 11, you can skip the separate compile step for a single-file program and run the source file directly:
java HelloWorld.java
A shorter version in newer Java
Java 25 (released in 2025) lets small programs leave out the class and much of the ceremony. This is a complete program in a file called Hello.java:
void main() {
IO.println("Hello, World!");
}
Run it with java Hello.java. It’s handy for learning and quick scripts, but the classic version above works on every version of Java and is what you’ll see in most existing code.