Introduction
The “Hello World” program is the first program most people write when learning a new programming language. It’s a simple program that displays “Hello, World!” on the screen. This helps verify that your Java environment is set up correctly.
The Hello World Program
Complete Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Output
Hello, World!
Understanding Each Part
1. public class HelloWorld
Breakdown:
public: Access modifier, means accessible from anywhereclass: Keyword to define a classHelloWorld: Name of the class (must match filename)
Rules:
- Class name must start with uppercase letter (convention)
- Class name should match the filename exactly
- File must be named
HelloWorld.java - Java is case-sensitive:
HelloWorld≠helloworld
2. Opening Brace {
- Marks the beginning of the class body
- Every opening brace must have a closing brace
- Everything inside belongs to the class
3. public static void main(String[] args)
This is the main method - entry point of the program.
Detailed Breakdown:
public:
- Access modifier
- Main method must be public so JVM can access it
- Can be called from outside the class
static:
- Method belongs to class, not to objects
- Can be called without creating an object
- JVM calls main() without creating class instance
void:
- Return type
- Means the method doesn’t return any value
- Main method returns nothing
main:
- Name of the method
- Special method name recognized by JVM
- JVM looks for this specific method to start execution
- Must be spelled exactly as “main”
String[] args:
- Parameter to the method
- Array of strings
- Holds command-line arguments
argsis just a variable name (can be any name)- Even if not used, must be present
Alternative Ways to Write:
public static void main(String args[]) // Also valid
public static void main(String... args) // Varargs (also valid)
4. System.out.println("Hello, World!");
This statement prints output to the console.
Breakdown:
System:
- Built-in Java class
- Located in
java.langpackage - Provides system-related functionality
out:
- Static member of System class
- Object of PrintStream class
- Represents standard output stream (console)
println:
- Method of PrintStream class
- Prints text and moves to next line
- “print line”
"Hello, World!":
- String literal (text)
- Enclosed in double quotes
- The actual message to display
; (Semicolon):
- Statement terminator
- Every statement in Java must end with semicolon
- Missing semicolon causes compilation error
5. Closing Braces }}
- First
}: Closes the main method - Second
}: Closes the class - Must match all opening braces
Step-by-Step: Writing the Program
Step 1: Open Text Editor or IDE
Choose any editor:
- Notepad (Windows)
- TextEdit (Mac)
- Notepad++
- VS Code
- IntelliJ IDEA
- Eclipse
Step 2: Write the Code
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Step 3: Save the File
- Filename:
HelloWorld.java(must match class name) - Location: Any folder (e.g., C:\JavaPrograms)
- Extension: Must be
.java - Important: Case-sensitive!
Step-by-Step: Compiling the Program
What is Compilation?
- Converts human-readable Java code (.java) to bytecode (.class)
- Bytecode can be executed by JVM
- Compiler is
javac(Java Compiler)
Step 1: Open Command Prompt/Terminal
Windows:
- Press Win + R
- Type
cmd - Press Enter
Mac/Linux:
- Open Terminal application
Step 2: Navigate to File Location
cd C:\JavaPrograms
Or wherever you saved the file.
Step 3: Compile Using javac
javac HelloWorld.java
What Happens:
javacis the Java compiler- Reads
HelloWorld.java - Checks for syntax errors
- Creates
HelloWorld.class(bytecode file)
Success:
- No message = successful compilation
HelloWorld.classfile created in same directory
Error Example:
HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")
^
1 error
If you get errors, check your code carefully!
Common Compilation Errors
Error 1: ‘javac’ is not recognized
'javac' is not recognized as an internal or external command
Solution: JDK not installed or PATH not set. Install JDK and set environment variables.
Error 2: Cannot find symbol
error: cannot find symbol
Solution: Typo in code, wrong class/method name.
Error 3: Class name mismatch
error: class HelloWorld is public, should be declared in a file named HelloWorld.java
Solution: Filename must match class name exactly.
Error 4: Missing semicolon
error: ';' expected
Solution: Add semicolon at end of statement.
Step-by-Step: Running the Program
Step 1: Ensure Compilation Success
- Check that
HelloWorld.classexists - No compilation errors
Step 2: Run Using java Command
java HelloWorld
Important Notes:
- Use
java, notjavac - Use class name, not filename
- Do NOT include .class extension
- Case-sensitive!
Correct:
java HelloWorld
Wrong:
java HelloWorld.class ❌
java HelloWorld.java ❌
java helloworld ❌
Step 3: See the Output
Hello, World!
Congratulations! Your first Java program works!
Common Runtime Errors
Error 1: Could not find or load main class
Error: Could not find or load main class HelloWorld
Causes:
- Class file doesn’t exist (compilation failed)
- Wrong directory
- Typo in class name
- Used .class extension
Error 2: Main method not found
Error: Main method not found in class HelloWorld
Cause: main method signature is wrong
Solution: Use exact signature: public static void main(String[] args)
Complete Process Summary
Visual Flow:
1. Write Code
↓
HelloWorld.java (source file)
↓
2. Compile
javac HelloWorld.java
↓
HelloWorld.class (bytecode)
↓
3. Run
java HelloWorld
↓
Output: Hello, World!
Commands Summary:
# Step 1: Navigate to directory
cd C:\JavaPrograms
# Step 2: Compile
javac HelloWorld.java
# Step 3: Run
java HelloWorld
# Output
Hello, World!
Variations of Hello World
1. Using print (without new line)
public class HelloWorld {
public static void main(String[] args) {
System.out.print("Hello, World!");
}
}
Output: Hello, World! (cursor stays on same line)
2. Multiple Outputs
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
System.out.println("Welcome to Java!");
System.out.println("This is my first program.");
}
}
Output:
Hello, World!
Welcome to Java!
This is my first program.
3. Using Escape Sequences
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello,\nWorld!");
}
}
Output:
Hello,
World!
4. Using Variables
public class HelloWorld {
public static void main(String[] args) {
String message = "Hello, World!";
System.out.println(message);
}
}
Using IDE (Easier Method)
IntelliJ IDEA:
- File → New → Project
- Choose Java
- Right-click src → New → Java Class
- Name it “HelloWorld”
- Write code
- Click green Run button
Eclipse:
- File → New → Java Project
- Right-click src → New → Class
- Name: “HelloWorld”
- Check “public static void main”
- Write code
- Click Run button
VS Code:
- Install Java Extension Pack
- Create HelloWorld.java file
- Write code
- Click “Run” at top right
What Happens Behind the Scenes?
Compilation Phase:
- Lexical Analysis: Break code into tokens
- Syntax Analysis: Check grammar
- Semantic Analysis: Check meaning
- Code Generation: Create bytecode
- Output: .class file created
Execution Phase:
- Class Loader: Loads HelloWorld.class
- Bytecode Verifier: Checks bytecode safety
- Interpreter: Converts bytecode to machine code
- JIT Compiler: Optimizes hot code
- Execution: Program runs
- Output: “Hello, World!” displayed
Important Points to Remember
- Class name = Filename:
HelloWorldclass needsHelloWorld.javafile - Case-sensitive: Java distinguishes between upper and lower case
- main() method: Entry point, must have exact signature
- Semicolons: Every statement must end with
; - Braces: Must match opening and closing
{} - Compilation:
javac HelloWorld.java - Execution:
java HelloWorld(no .class) - Output:
System.out.println()to print
Exam Tips
Remember:
-
Main method signature:
public static void main(String[] args)Every part is necessary!
-
Compilation vs Execution:
javac= compile (creates .class)java= run (executes .class)
-
File naming:
- Filename must match public class name
- Case-sensitive
- .java extension
-
print vs println:
print(): No new line afterprintln(): New line after
-
Common errors:
- Missing semicolon
- Wrong main method signature
- Filename mismatch
- Case sensitivity issues
Likely Exam Questions:
- Write a Hello World program
- Explain each part of the main method
- What is the difference between javac and java?
- Why is main method static?
- What happens during compilation?
- What is bytecode?
- Explain System.out.println()
Practice Exercises
Exercise 1: Personal Greeting
Write a program that prints:
Hello, [Your Name]!
Welcome to Java Programming!
Exercise 2: Multiple Lines
Write a program that prints:
*****
*****
*****
Exercise 3: With Variables
Write a program that stores your name in a variable and prints it.
Summary
The Hello World program teaches us:
- Basic structure of Java program
- How to compile and run Java code
- Understanding of main method
- How to display output
- Foundation for all Java programs