Nearby lessons

8 of 125

Java - Hello World Program

📌 What You Will Learn
  • 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:

Example01
JCode Cell
1class First {
2 public static void main(String[] args) {
3 System.out.println("Hello Java!");
4 }
5}

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:

Example02
JCode Cell
1First.java

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.

Example03
JCode Cell
1C:\Users\admin\Desktop\JS> javac First.java

Writing Your First Java Program

Congratulations! You have written, compiled and run your first Java program.

Example04
JCode Cell
1C:\Users\admin\Desktop\JS> java First
Output
Hello Java!

Understanding the Program Line by Line

Program partWhat it means
class FirstWe 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:

WordMeaning
publicThis method can be called from anywhere. The JVM needs to reach it, so it must be public.
staticWe can call this method without creating an object of the class. The JVM calls it before any object exists.
voidThis method does not return any value back.
mainThis is the method name. It is a keyword-like name reserved for the entry point.
String[] argsA parameter — an array of String values. It can receive command-line arguments.
In simple words: Java always starts running from the main method. The JVM looks for public static void main(String[] args) before anything else, so it must be written exactly right.
Trainer's Note: In older books you may see main(String args[]). Both forms are valid — String[] args and String args[] mean the same thing. Use String[] args, which is the modern style.
Example06
JCode Cell
1public static void main(String[] args)
2 | | | | |
3 | | | | +-- receives any values typed after 'java First' on the command line
4 | | | +-- the name of the method
5 | | +-- returns nothing
6 | +-- called without making an object
7 +-- accessible from anywhere (JVM can call it)

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).
In simple words: The file name must match the public class name exactly — same spelling, same capitals. If the class is MyProgram, the file must be MyProgram.java or the compiler refuses to run.
Example07
JCode Cell
1File name: MyProgram.java
2 
3public class MyProgram { // OK - public class, file named after it
4 public static void main(String[] args) {
5 System.out.println("File name rule");
6 }
7}

Common Errors and Their Solutions

ErrorWhy it happensSolution
'javac' is not recognizedPATH not setSet PATH (Section 2.2) or use full path to javac.
cannot find file: First.javaYou are in the wrong folderUse 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 FirstYou typed java First.class or wrong folderRun java First (no .class, no .java). Be in the correct folder.
class First is public, should be declared in a file named First.javaFile name mismatchRename the file to First.java.
';' expectedMissing semicolonEnd every statement with a semicolon.
'main' method not foundmain spelled or written wronglyWrite exactly: public static void main(String[] args)
Cannot find symbolWrong spelling of a nameCheck class/variable names carefully. Java is case sensitive.
📝 Key Takeaways
  • 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.

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2