Writing Running Hello World Program

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 anywhere
  • class: Keyword to define a class
  • HelloWorld: 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: HelloWorldhelloworld

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
  • args is 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.lang package
  • 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:

  • javac is the Java compiler
  • Reads HelloWorld.java
  • Checks for syntax errors
  • Creates HelloWorld.class (bytecode file)

Success:

  • No message = successful compilation
  • HelloWorld.class file 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.class exists
  • No compilation errors

Step 2: Run Using java Command

java HelloWorld

Important Notes:

  • Use java, not javac
  • 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:

  1. File → New → Project
  2. Choose Java
  3. Right-click src → New → Java Class
  4. Name it “HelloWorld”
  5. Write code
  6. Click green Run button

Eclipse:

  1. File → New → Java Project
  2. Right-click src → New → Class
  3. Name: “HelloWorld”
  4. Check “public static void main”
  5. Write code
  6. Click Run button

VS Code:

  1. Install Java Extension Pack
  2. Create HelloWorld.java file
  3. Write code
  4. Click “Run” at top right

What Happens Behind the Scenes?

Compilation Phase:

  1. Lexical Analysis: Break code into tokens
  2. Syntax Analysis: Check grammar
  3. Semantic Analysis: Check meaning
  4. Code Generation: Create bytecode
  5. Output: .class file created

Execution Phase:

  1. Class Loader: Loads HelloWorld.class
  2. Bytecode Verifier: Checks bytecode safety
  3. Interpreter: Converts bytecode to machine code
  4. JIT Compiler: Optimizes hot code
  5. Execution: Program runs
  6. Output: “Hello, World!” displayed

Important Points to Remember

  1. Class name = Filename: HelloWorld class needs HelloWorld.java file
  2. Case-sensitive: Java distinguishes between upper and lower case
  3. main() method: Entry point, must have exact signature
  4. Semicolons: Every statement must end with ;
  5. Braces: Must match opening and closing {}
  6. Compilation: javac HelloWorld.java
  7. Execution: java HelloWorld (no .class)
  8. Output: System.out.println() to print

Exam Tips

Remember:

  1. Main method signature:

    public static void main(String[] args)

    Every part is necessary!

  2. Compilation vs Execution:

    • javac = compile (creates .class)
    • java = run (executes .class)
  3. File naming:

    • Filename must match public class name
    • Case-sensitive
    • .java extension
  4. print vs println:

    • print(): No new line after
    • println(): New line after
  5. Common errors:

    • Missing semicolon
    • Wrong main method signature
    • Filename mismatch
    • Case sensitivity issues

Likely Exam Questions:

  1. Write a Hello World program
  2. Explain each part of the main method
  3. What is the difference between javac and java?
  4. Why is main method static?
  5. What happens during compilation?
  6. What is bytecode?
  7. 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