Your First Java Program — Hello World Explained

4 minute read

This is the chapter you’ve been waiting for — you’re about to write your very first Java program. We’ll go slow and explain every single word so nothing feels magical or confusing.


Step 1: Create the File

Create a new file called HelloWorld.java in your java-projects folder.

Important: The filename must exactly match the class name inside (including capitalization). The file is HelloWorld.java because the class is HelloWorld.


Step 2: Write the Code

Type this exactly (don’t copy-paste — typing builds muscle memory):

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

Step 3: Compile and Run

Open your terminal, navigate to the file’s location, and run:

$ javac HelloWorld.java
$ java HelloWorld

Expected output:

Hello, World!

🎉 Congratulations! You just wrote, compiled, and ran your first Java program.


Understanding Every Word

Let’s break down every piece of that 5-line program. Nothing should be mysterious.

Line 1: public class HelloWorld {

public class HelloWorld {
Word Meaning
public This class is accessible from anywhere
class You’re defining a class (Java’s basic building block)
HelloWorld The name of your class (must match the filename)
{ Opening brace — the class body starts here

Think of a class as a container for your code. Every Java program must have at least one class.

Line 2: public static void main(String[] args) {

This is the main method — the entry point of every Java program. When you run java HelloWorld, the JVM looks for this exact method signature and starts executing there.

public static void main(String[] args) {
Word Meaning
public Accessible from anywhere (the JVM needs to call it)
static Belongs to the class itself, not to an object (you’ll understand this fully in OOP)
void This method doesn’t return any value
main The method name — must be exactly “main”
String[] args An array of strings for command-line arguments
{ The method body starts here

For now, just remember: Every Java program needs public static void main(String[] args). It’s the starting line.

Line 3: System.out.println("Hello, World!");

This is the line that actually does something — it prints text to the screen.

System.out.println("Hello, World!");
Part Meaning
System A built-in Java class for system-level operations
.out The standard output stream (your terminal/console)
.println() A method that prints a line of text (and adds a newline at the end)
"Hello, World!" A string literal — the text to print
; Every Java statement ends with a semicolon

Lines 4–5: Closing Braces

    }
}

The first } closes the main method. The second } closes the HelloWorld class.


What Happens Behind the Scenes?

┌──────────────────┐
│  HelloWorld.java │  ← You write this (source code)
└────────┬─────────┘
         │  javac HelloWorld.java
         ▼
┌──────────────────┐
│  HelloWorld.class│  ← Compiler creates this (bytecode)
└────────┬─────────┘
         │  java HelloWorld
         ▼
┌──────────────────┐
│  Hello, World!   │  ← JVM executes and prints this
└──────────────────┘
  1. javac HelloWorld.java — The compiler reads your source code and creates HelloWorld.class (bytecode)
  2. java HelloWorld — The JVM reads the bytecode and executes it

Notice: you compile with javac HelloWorld.java (with .java) but run with java HelloWorld (without any extension).


Exploring the .class File

After compiling, check your folder:

$ ls
HelloWorld.class  HelloWorld.java

The .class file contains bytecode — not human-readable. If you try to open it in a text editor, you’ll see gibberish. That’s intentional — it’s designed for the JVM, not for humans.


Common Mistakes and How to Fix Them

Mistake 1: Filename Doesn’t Match Class Name

// File: hello.java  ← Wrong! Should be HelloWorld.java
public class HelloWorld {

Error:

error: class HelloWorld is public, should be declared in a file named HelloWorld.java

Fix: Rename the file to HelloWorld.java.

Mistake 2: Missing Semicolon

System.out.println("Hello, World!")  // ← Missing ;

Error:

error: ';' expected

Fix: Add ; at the end of the statement.

Mistake 3: Wrong Capitalization

system.out.println("Hello");  // ← 's' should be capital 'S'

Error:

error: cannot find symbol

Fix: Java is case-sensitive. It’s System, not system.

Mistake 4: Wrong Quotes

System.out.println('Hello, World!');  // ← Single quotes are for single characters

Error:

error: unclosed character literal

Fix: Use double quotes "..." for strings.

Mistake 5: Running with .class Extension

$ java HelloWorld.class  # ← Wrong!

Error:

Error: Could not find or load main class HelloWorld.class

Fix: Run with java HelloWorld (no extension).


Experiments to Try

Try these modifications to build intuition:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
        System.out.println("My name is Java.");
        System.out.println("I was born in 1995.");
    }
}

Output:

Hello, World!
My name is Java.
I was born in 1995.
System.out.print("Hello, ");
System.out.print("World!");
System.out.println();  // Just adds a newline

Output:

Hello, World!
System.out.println(42);
System.out.println(3 + 7);
System.out.println("The answer is: " + 42);

Output:

42
10
The answer is: 42

Running from IntelliJ vs Command Line

Method How
Command line javac HelloWorld.java then java HelloWorld
IntelliJ Click green ▶ button next to main
VS Code Click “Run” above main method

Behind the scenes, IDEs do exactly the same thing — compile and run. They just automate it.


Try It Yourself

Write a program called AboutMe.java that prints:

My name is [Your Name].
I am learning Java.
Java was created in 1995 by James Gosling.
Today I wrote my first Java program!

Compile and run it. If it works, you’re ready for the next chapter!


Summary

Concept Key Point
Every program needs public static void main(String[] args)
Filename must match Class name (case-sensitive)
Print text System.out.println("text")
Compile javac FileName.java
Run java ClassName (no extension)
Statements end with ; (semicolon)
Java is Case-sensitive — System ≠ system

What’s Next?

Now that you can write and run Java programs, let’s learn how to store and work with data:

Java Variables →

Updated: