Nearby lessons
8 of 125Java - Hello World Program
- How to set up Java (PATH) on your computer
- How to write, save, compile and run your first program
- The meaning of every word in the main method
- The rules about class name and file name
- How to fix the common errors a beginner faces
Hello World Program is a core concept of the Java language. This lesson explains Writing Your First Java Program, Understanding the Program Line by Line and The main Method — Word by Word with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
Writing Your First Java Program
Open Notepad (or your editor) and type this program exactly:
Now we go through the three steps to run it.
Step 4 — Save the file
Save the file with the exact class name and the .java extension. Here the class name is First, so save it as:
Writing Your First Java Program
File name and class name must match exactly (same spelling, same capitals). In Windows, remember to choose All Files as the save type, so that the file does not become First.java.txt.
Step 5 — Compile
Open the command prompt, go to the folder where you saved the file, and type:
Writing Your First Java Program
If everything is correct, the command prompt simply returns to the next line (no message). Now check the folder — a new file First.class is created. This is the bytecode file.
Step 6 — Run
Type this command. Note: we give only the class name, without the .class extension.
Writing Your First Java Program
Congratulations! You have written, compiled and run your first Java program.
Understanding the Program Line by Line
| Program part | What it means |
|---|---|
| class First | We are declaring a class (a blueprint) with the name First. |
| { | Opening curly brace — the body of the class starts here. |
| public static void main(String[] args) | This is the main method. Java always starts execution from here. |
| System.out.println(...) | Prints the message on the screen and moves the cursor to a new line. |
| ; | Semicolon — in Java, every statement must end with a semicolon. |
| } and } | Closing braces — end of the main method, end of the class. |
The main Method — Word by Word
Every Java program needs a starting point. That starting point is the main method. Let us understand each word, because interviewers love this question:
| Word | Meaning |
|---|---|
| public | This method can be called from anywhere. The JVM needs to reach it, so it must be public. |
| static | We can call this method without creating an object of the class. The JVM calls it before any object exists. |
| void | This method does not return any value back. |
| main | This is the method name. It is a keyword-like name reserved for the entry point. |
| String[] args | A parameter — an array of String values. It can receive command-line arguments. |
Rules About Class Name and File Name
- In one file, only one class can be public. The file must be named after that public class.
- If no class is public, the file can be named after any class in it.
- Java is case sensitive. First and first are different names. File First.java cannot contain class first.
- A class name should start with a capital letter (coding habit, not a rule).
Common Errors and Their Solutions
| Error | Why it happens | Solution |
|---|---|---|
| 'javac' is not recognized | PATH not set | Set PATH (Section 2.2) or use full path to javac. |
| cannot find file: First.java | You are in the wrong folder | Use cd to reach the folder where First.java is saved, or save the file in the current folder. |
| Could not find or load main class First | You typed java First.class or wrong folder | Run java First (no .class, no .java). Be in the correct folder. |
| class First is public, should be declared in a file named First.java | File name mismatch | Rename the file to First.java. |
| ';' expected | Missing semicolon | End every statement with a semicolon. |
| 'main' method not found | main spelled or written wrongly | Write exactly: public static void main(String[] args) |
| Cannot find symbol | Wrong spelling of a name | Check class/variable names carefully. Java is case sensitive. |
- Six steps: install JDK → choose editor → write program → save file → compile with javac → run with java.
- File name must match the public class name exactly, and must end with .java.
- javac creates a .class file (bytecode); java runs it.
- public static void main(String[] args) is the entry point of every program.
- Comments (//, /* */, /** */) are ignored by the compiler and are used for documentation.
- Most beginner errors are due to PATH, folder location, or spelling mistakes.