Every Java developer's journey starts with the same eleven lines of code, and understanding those lines completely is worth more than typing a hundred programs you don't understand. This tutorial writes Hello World, then dissects every keyword so nothing is "just how you do it."
You need a working JDK before you start. If java -version does not print a version in your terminal, stop and follow how to install Java first, then come back.
Write the program
Open a plain text editor and create a file named exactly HelloWorld.java. The capitalization matters — we will see why in a moment.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
That is a complete Java program. Save it, and make sure the file name is HelloWorld.java, not helloworld.java or HelloWorld.java.txt (a common trap when an editor hides extensions).
Compile and run it
Java is a compiled language, so there are two steps: turn source into bytecode, then run the bytecode. Open a terminal in the folder containing your file.
javac HelloWorld.java # step 1: compile -> creates HelloWorld.class
java HelloWorld # step 2: run the bytecode
The first command, javac, produces a new file called HelloWorld.class containing bytecode. The second command, java, hands that bytecode to the JVM, which executes it and prints:
Hello, World!
Common mistake: Typing
java HelloWorld.classin step two. Thejavalauncher wants a class name, not a file name, so you writejava HelloWorldwith no extension. Adding.classgives you a "could not find or load main class" error that confuses almost every beginner once.
Notice the workflow: compile once, run as many times as you like. Change the source, and you must re-run javac before the change takes effect. That two-step rhythm is Java's compiled nature in action — the same reason Java code is portable across machines is explained in what is Java.
Understand every line
The magic disappears once you can name what each part does. Let's go line by line.
public class HelloWorld
This declares a class named HelloWorld. In Java, all code lives inside a class — there are no free-floating functions like in some languages. The word public is an access modifier meaning any code anywhere may use this class.
This is why the file name must match: a public class named HelloWorld must live in a file named HelloWorld.java. The compiler enforces it so that any class can be found by its name. Rename the class to Greeting without renaming the file and you get a compile error immediately.
public static void main(String[] args)
This is the entry point — the exact method the JVM calls to start your program. Every word is load-bearing:
public— the JVM, which is outside your class, must be able to call it.static— it belongs to the class, not to an object, so the JVM can call it without first creating aHelloWorldinstance. You will understand this deeply after reading about the static keyword.void— the method returns nothing to the JVM.main— the fixed name the JVM looks for. Spell itMainormianand the program compiles but refuses to run.String[] args— an array that receives any arguments you pass on the command line.
Change any part of that signature and the JVM will not find a valid entry point.
System.out.println("Hello, World!")
This prints text and moves to a new line. Reading it right to left: System is a built-in class, out is its standard-output stream, and println is the method that writes a line. The text in quotes is a String literal — the argument you are printing.
Pro tip:
printlnadds a newline after printing;System.out.printwhen you want the next output on the same line. This small distinction shows up constantly once you start building formatted output.
A second program: use the arguments
Hello World never uses String[] args, which makes that parameter feel pointless. Here is a program that actually reads command-line input, so the pieces connect.
public class Greeter {
public static void main(String[] args) {
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello! Pass your name as an argument.");
}
}
}
Compile and run it with an argument:
javac Greeter.java
java Greeter Anitha # prints: Hello, Anitha!
Run it again as java Greeter with no argument and you get the fallback message. Now String[] args is not abstract — args[0] is the first word you typed after the class name, and args.length tells you how many you passed. The + operator here joins strings together, which you will use constantly; the details are in the strings tutorial.
Common errors and what they really mean
These four cover almost every "it won't run" message from a beginner:
class HelloWorld is public, should be declared in a file named HelloWorld.java— your file name and public class name disagree. Rename the file to match.could not find or load main class HelloWorld— you ranjavafrom the wrong folder, added.class, or the class is inside a package. Run it from the folder holding the.classfile.javac: command not found— the JDK is not on your PATH. Revisit the install guide; this means the compiler isn't reachable.error: ';' expected— you forgot a semicolon. Every statement in Java ends with one.
Reading the error text instead of panicking is a real skill. The compiler almost always tells you the file, the line, and the problem in plain language. When you hit an error, read it top to bottom: the first error is usually the real one, and the later ones are often just knock-on effects of it. Fix the top error, recompile, and repeat.
Compile-time vs runtime errors
It helps to know which stage failed. A compile-time error comes from javac — a missing semicolon, a misspelled type, a file-name mismatch — and it stops you before any code runs. A runtime error comes from java while the program executes, like a NullPointerException or dividing by zero. The distinction matters because the fix is different: compile errors are about syntax and structure, runtime errors are about the logic and data your program actually meets while running.
Why this matters beyond Hello World
The structure you just wrote — a class, a main method, statements ending in semicolons — is the skeleton of every Java program you will ever write, from a console tool to a Spring Boot service. The public static void main signature in particular is a favorite fresher interview question precisely because it packs four concepts into one line: access modifiers, the static keyword, return types, and arrays.
A question worth answering now: why compile at all, instead of just running the source? Java compiles your .java file into platform-independent bytecode (.class), and the JVM on any operating system runs that same bytecode. That two-step design is what "write once, run anywhere" means in practice — you compile once and the result runs unchanged on Windows, macOS, or Linux. It is also why you never ship .java files to run; you ship the compiled .class files (usually bundled in a .jar).
Once you can compile and run confidently, the next building block is storing data. Move on to variables in Java and then data types to start writing programs that actually compute something. When you are ready to go from isolated syntax to building real applications with guidance, the Java Full Stack course takes you from this first program all the way to deployed projects, and the full Java learning hub maps every topic in between.
Frequently Asked Questions
Why must the file name match the class name in Java?
What does public static void main(String[] args) mean?
What is the difference between javac and java?
Why do I get 'could not find or load main class'?
Do I need an IDE to run my first Java program?
Want to Build Your Career in Java Full Stack with AI?
Join CodeBegun and train with working industry engineers — Explore the Program

