Hello World Program in Java
In this lesson: Write your first Java program — class, main method, System.out.println — explained line by line with output. In Hindi & English.
Your first program
The classic first program in any language prints "Hello, World!". In Java it looks longer than in some languages, because every Java program must live inside a class and start from the main method. Let us write it and understand every part.
main method.Step 0: install the JDK and check it
You need the JDK (Java Development Kit), not only the JRE, because it contains the compiler javac. Install a current LTS version (for example Java 21 or 25) from Eclipse Temurin (adoptium.net) or Oracle, then open a new Command Prompt/Terminal and check both tools:
java -version javac -version
(Output from Java 21 on Linux; your version and build text will differ.)
If you see 'javac' is not recognized on Windows, the JDK bin folder is not in PATH. Re-run the installer with “Set JAVA_HOME / Add to PATH” ticked, or add C:\Program Files\Eclipse Adoptium\jdk-21…\bin to PATH and open a new window.
The Hello World code
public class Hello {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello, World!
Save this as Hello.java. It looks like a lot for one line of output, but each part has a clear job, which we break down next.
Line by line
public class Hello— declares a class named Hello. The file must beHello.java.public static void main(String[] args)— the starting point of the program.System.out.println("Hello, World!")— prints the text and moves to a new line.{ }— curly braces group the code that belongs to the class and the method.
Everything you want the program to do goes inside main. When you run the program, Java looks for main and starts there.
Compile and run
javac Hello.java // compile -> creates Hello.class java Hello // run (no .class)
javac turns your code into bytecode, and java Hello runs it. Notice you run java Hello, not java Hello.class.
Shortcut: run the source file directly
Since Java 11 you can run a single-file program without a separate compile step. Java compiles it in memory and runs it:
java Hello.java
This is perfect for practice programs. For exams and real projects still learn javac + java, because bigger programs have many files.
Real error messages and what they mean
These are the exact messages the Java 21 compiler printed when we made the most common beginner mistakes:
1. Missing semicolon
The number after the file name (:3) is the line. The caret ^ points where Java expected the ;.
2. File saved as hello.java (small h)
3. system instead of System
Java is case-sensitive: System is a class, system means nothing.
4. Main instead of main – compiles, but fails when run
Terminal, IDE or online compiler?
| Option | Best for | Note |
|---|---|---|
| Terminal (javac + java) | Understanding how Java really runs; board practicals | Needs JDK + PATH |
| VS Code + “Extension Pack for Java” | Students, light laptops | Run button, error underlines |
| IntelliJ IDEA Community / Eclipse | Projects with many files, Spring Boot | Heavier, more features |
| Online compiler | Phone or a computer where you cannot install | File/class name may be fixed to Main |
Newer Java: an even shorter Hello World
Java 25 added compact source files and instance main methods, so a beginner program can be written without the class line:
void main() {
IO.println("Hello, World!");
}
It needs Java 25 or later. Most textbooks, CBSE/university practicals and interviews still use the classic public class … public static void main(String[] args) form, so learn that first.
print vs println
System.out.print("Hello ");
System.out.print("World");
System.out.println(); // new line
System.out.println("Next line");
Next line
print stays on the same line, while println moves to a new line after printing. Use println when you want each message on its own line.
Common mistakes
- File name mismatch.
class Hellomust be saved asHello.java. - Missing semicolon. Every statement ends with
;. - Wrong main signature. It must be exactly
public static void main(String[] args). - Running with
.class. Usejava Hello, notjava Hello.class.
Summary
- Every Java program lives in a class and starts from
main. System.out.printlnprints a line of text.- The file name must match the public class name.
- Compile with
javac File.java, run withjava File. printstays on the line;printlnmoves to a new line.
Next, store data with Java variables, or set up your tools in the Java setup guide.
Frequently Asked Questions
What is the structure of a basic Java program?
main method where execution starts. The code you want to run goes inside main. Even a one-line program needs this class and main wrapper.What does public static void main(String[] args) mean?
public makes it accessible, static lets it run without creating an object, void means it returns nothing, and String[] args holds any command-line inputs.What does System.out.println do?
System.out is the standard output, and println means "print line".Why must the file name match the class name?
Hello, the file must be Hello.java. Otherwise the compiler gives an error.What is the difference between print and println?
print shows text and stays on the same line. println shows text and then moves to the next line. Use println when you want each output on its own line.