
Every programmer’s first program prints “Hello, World!” to the screen. It’s a tradition that dates back to a 1978 book on the C programming language. The program itself does almost nothing, but writing it proves your development environment works and gives you a foundation to build on.
If you haven’t installed Java yet, follow our installation guide first. You’ll need the JDK set up and working before anything here will run.
The Complete Program
Here’s the full code. Create a new file called HelloWorld.java and type this exactly:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Five lines. One of them is just a closing brace. But each piece serves a purpose, and understanding them now saves confusion later.
Breaking Down Each Line
Line 1: The Class Declaration
public class HelloWorld {
public means this class is accessible from anywhere. For now, just include it. The reasons why will make more sense when you learn about packages and access modifiers.
class tells Java you’re defining a class. In Java, all code lives inside classes. You can’t write loose code floating in a file like you can in Python or JavaScript.
HelloWorld is the name you chose for this class. It must match the filename exactly, including capitalization. If your file is HelloWorld.java, the class must be HelloWorld. Not helloworld. Not Helloworld. Java cares about case.
The opening brace { marks the start of the class body. Everything belonging to this class goes between this brace and its matching closing brace on line 5.
Line 2: The Main Method
public static void main(String[] args) {
This line has more going on. Take it piece by piece:
public again means accessible from anywhere. The Java runtime needs to call this method from outside your class, so it must be public.
static means this method belongs to the class itself, not to a specific object created from the class. When you run a Java program, no objects exist yet. The main method needs to run without one, so it must be static.
void means this method doesn’t return any value. It does its work and finishes. Some methods calculate and return results. Main just executes and ends.
main is the method name. This isn’t a choice – Java looks for a method named exactly main as the entry point. Spell it differently and your program won’t run.
String[] args is a parameter that holds command-line arguments. When someone runs your program with extra words after the command, those words arrive here as an array of strings. You won’t use this in simple programs, but it must be present. The parameter name args is a convention. You could call it arguments or params or anything else, though args is what most Java code uses.
Line 3: The Print Statement
System.out.println("Hello, World!");
System is a built-in class that comes with Java. It provides access to system-level resources like standard input, standard output, and error streams.
out is a static field inside System that represents standard output (your terminal or console window).
println is a method that prints text and adds a newline at the end. There’s also print (no newline) and printf (formatted output), but println is the most common for simple output.
"Hello, World!" is a string literal. The double quotes mark its boundaries. Whatever text you put between them gets printed.
The semicolon ends the statement. Every statement in Java ends with a semicolon. Forget one and the compiler complains.
Lines 4 and 5: Closing Braces
}
}
The first brace closes the main method. The second closes the class. Every opening brace needs a matching closing brace. Indentation helps you track which brace belongs to which block, but Java itself doesn’t care about whitespace – it reads the braces.
Compiling and Running
Save your file, open a terminal, and navigate to the folder containing HelloWorld.java.
Compile the program:
javac HelloWorld.java
If successful, this command produces no output. Silence means it worked. You’ll find a new file called HelloWorld.class in the same folder. This is the compiled bytecode.
Run the program:
java HelloWorld
Notice you use javac (the Java compiler) with the .java extension, but java (the runtime) without any extension. The runtime takes the class name, not the filename.
Output:
Hello, World!
That’s it. You wrote, compiled, and ran a Java program.
Common Errors and How to Fix Them
Error: class HelloWorld is public, should be declared in a file named HelloWorld.java
Your filename doesn’t match your class name. If the class is public class HelloWorld, the file must be exactly HelloWorld.java. Check for typos and capitalization.
Error: cannot find symbol: System
You probably typed system with a lowercase ‘s’. Java is case-sensitive. It’s System, not system.
Error: ‘;’ expected
You forgot a semicolon somewhere. The error message tells you which line. Check that line and the one before it.
Error: reached end of file while parsing
You’re missing a closing brace. Count your opening and closing braces. They should match.
Error: ‘class’ or ‘interface’ expected
Something is wrong with your class declaration. You might have typed Class instead of class, or you have code outside the class body.
Error: Main method not found
The program compiled, but Java can’t find the entry point. Check that your main method signature is exactly public static void main(String[] args). A missing static, a typo in main, or string instead of String causes this.
Variations to Try
Modify the program to print something else:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("My name is Alice.");
System.out.println("I am learning Java.");
}
}
Each println produces a separate line of output. Run this and you get:
My name is Alice.
I am learning Java.
Try print instead of println:
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello, ");
System.out.print("World!");
System.out.println();
}
}
Output:
Hello, World!
The two print calls put text on the same line. The empty println() at the end just adds a newline.
Adding Comments
Comments let you leave notes in your code. Java ignores them completely.
// This is a single-line comment
public class HelloWorld {
public static void main(String[] args) {
// Print a greeting to the console
System.out.println("Hello, World!");
}
}
/*
This is a multi-line comment.
It can span several lines.
Useful for longer explanations.
*/
Use // for brief notes on a single line. Use /* */ for longer explanations that span multiple lines. Don’t overdo it. Good code is mostly self-explanatory. Comments should explain why, not what.
A Note on IDEs
Professional developers rarely compile from the command line. They use Integrated Development Environments like IntelliJ IDEA, Eclipse, or VS Code with Java extensions. These tools compile automatically, highlight errors as you type, and offer features like code completion.
But learning to compile manually teaches you what’s actually happening. When an IDE does something mysterious, you’ll understand the underlying process. Consider the command line a training tool. Graduate to an IDE once the fundamentals feel solid.
What the Program Taught You
This tiny program introduced several concepts you’ll use constantly:
Classes contain all Java code. The main method is where execution starts. Statements end with semicolons. Braces define code blocks. System.out.println writes to the console. And Java is unforgiving about exact spelling and capitalization.
The next step is learning to store and manipulate data using variables. That’s where programs start doing useful work.
Related: What is Java? | How to Install Java | Java Variables and Data Types
Sources
- Kernighan, Brian W. and Dennis M. Ritchie. “The C Programming Language.” Prentice Hall, 1978.
- Oracle. “The Java Tutorials: Getting Started.” docs.oracle.com


