Java Tutorial
🔍

First Java Program

Every Java developer wrote this program first.

It doesn't do much — it prints one line of text to the screen. But inside those five lines is the entire skeleton of every Java program you will ever write. Understand this, and the structure of Java clicks into place.

👉 What you'll learn here:

  • The complete structure of a Java program
  • What every single line means — in plain English
  • How to compile and run it yourself
  • The most common mistakes beginners make
  • How to modify it and experiment

🧘 If this is your very first time looking at code — don't panic.

Every line will be explained. You don't need to memorize anything right now. Just read through once to get the shape of it, then read again with the explanations. Understanding builds up in layers — not all at once.

The Program

Here it is. Five lines. Your starting point for everything that follows.

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

Output:

Hello, World!

That's the whole program. Now let's understand every part of it.

💡 Try this before reading on: Look at the code and see if you can identify:

  • Where the class starts and ends
  • Where the main method is
  • Which line actually prints the output

Don't worry if you can't — the explanation below covers all three.

Line by Line — What Everything Means

Line 1: public class HelloWorld {

This line declares a class named HelloWorld.

In Java, everything lives inside a class. There is no such thing as a Java program without at least one class. Think of a class as a container — a named box that holds all your code.

Breaking down each word:

  • public — this class can be accessed from anywhere. It's visible to the whole program.
  • class — this keyword tells Java: "I'm defining a class."
  • HelloWorld — this is the name you're giving the class. You choose it.
  • { — this opening brace marks the start of the class body. Everything between { and the matching } belongs to this class.

⚠️ Critical rule: The class name must exactly match the filename. Since the class is named HelloWorld, the file must be saved as HelloWorld.java — same capitalization, same spelling. If they don't match, the compiler refuses to compile.

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

This line declares the main method — the entry point of every Java program.

When you run a Java program, the JVM doesn't know where to start. It looks for exactly this method — public static void main(String[] args) — and begins execution there. If this method doesn't exist, the program cannot run.

Think of it as Java's "Start" button. Every Java application has exactly one.

Breaking down each word:

  • public — the JVM needs to be able to call this method from outside the class. Making it public allows that.
  • static — the JVM calls main before creating any objects. static means the method belongs to the class itself, not to an instance of it. You'll understand this fully when you learn OOP.
  • void — this method doesn't return any value. It just runs and finishes.
  • main — this is the specific name the JVM looks for. It must be spelled exactly as main.
  • (String[] args) — this is the parameter list. args is an array of Strings passed in from the command line. You're not using it right now — but it must be there.
  • { — opens the method body. Everything between this { and its matching } is what runs when the program starts.

💡 For now, treat public static void main(String[] args) as a magic phrase. You'll write it hundreds of times before you fully understand every word — and that's fine. Its meaning becomes clear naturally as you learn more Java.

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

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

Breaking it down piece by piece:

  • System — a built-in Java class that gives you access to system-level resources. It represents your computer's system.
  • .out — a property of System that represents the standard output stream — think of it as the channel connected to your terminal screen.
  • .println — a method that prints a line of text and then moves the cursor to the next line. "println" = "print line."
  • ("Hello, World!") — the text you want to print, wrapped in double quotes. This is called a String literal.
  • ; — the semicolon ends the statement. Every statement in Java ends with a semicolon. Forget it and the compiler will tell you immediately.

💡 print vs println: There are two versions:

  • System.out.println("Hello") — prints the text and moves to a new line
  • System.out.print("Hello") — prints the text and stays on the same line

Most of the time you'll use println.

Lines 4 and 5: } and }

These two closing braces close what the opening braces on lines 1 and 2 opened.

  • The } on line 4 closes the main method
  • The } on line 5 closes the HelloWorld class

Every opening { in Java must have a matching closing }. If they don't match — too many, too few, or in the wrong place — the compiler throws an error.

💡 Indentation tip: Notice the code is indented — main is indented inside the class, and System.out.println is indented inside main. Java doesn't require this indentation — the program runs without it. But it makes the nesting structure instantly readable. Always indent your code.

The Full Structure — Visualized

Here's the same program with annotations showing what each part does:

ANATOMY OF A JAVA PROGRAM public class HelloWorld { ← Class declaration public static void main(String[] args) { ← Entry point System.out.println("Hello, World!"); Prints text to the screen and moves to the next line ← Statement (ends with ;) } ← Closes main method } ← Closes HelloWorld class

The nested structure is the key mental model:

  • The class is the outermost container
  • The method lives inside the class
  • The statements (actual instructions) live inside the method

How to Compile and Run It

If you haven't installed Java yet, go to Install Java & Setup first.

Step 1 — Save the file

Create a file named HelloWorld.java and paste in the program exactly as shown. The filename is case-sensitive — helloworld.java or Hello_World.java will both fail.

Step 2 — Open your terminal

Navigate to the folder where you saved the file:

cd path/to/your/folder

Step 3 — Compile

javac HelloWorld.java

No output = success. A new file called HelloWorld.class appears in the same folder. That's the compiled Bytecode.

Step 4 — Run

java HelloWorld

Output:

Hello, World!

⚠️ Common mistake: Running java HelloWorld.java instead of java HelloWorld. The java command takes the class name, not the filename. Drop the .java extension.

Now Experiment — Modify the Program

The best way to learn is to break things and fix them. Try each of these:

Change the message:

Java
1System.out.println("Hello, DevStackFlow!");

Recompile with javac HelloWorld.java and run again. The output changes.

Print multiple lines:

Java
1public class HelloWorld { 2 public static void main(String[] args) { 3 System.out.println("Line one"); 4 System.out.println("Line two"); 5 System.out.println("Line three"); 6 } 7}

Output:

Line one Line two Line three

Each println call prints one line and moves to the next.

Use print instead of println:

Java
1public class HelloWorld { 2 public static void main(String[] args) { 3 System.out.print("Hello, "); 4 System.out.print("World!"); 5 } 6}

Output:

Hello, World!

Both print statements appear on the same line because print doesn't add a newline.

Print a number:

Java
1System.out.println(42); 2System.out.println(3.14);

Output:

42 3.14

println works with numbers too — no quotes needed for numbers.

The Most Common Beginner Mistakes

Every beginner makes these. Knowing them in advance saves frustration.

1. Filename doesn't match class name

Java
1// File is saved as helloworld.java 2public class HelloWorld { // ← class is HelloWorld

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

Fix: Rename the file to HelloWorld.java exactly.

2. Missing semicolon

Java
1System.out.println("Hello, World!") // ← no semicolon

Error: error: ';' expected

Fix: Add ; at the end of every statement.

3. Wrong capitalization

Java
1system.out.println("Hello"); // ← lowercase 's' 2System.Out.println("Hello"); // ← uppercase 'O' 3System.out.Println("Hello"); // ← uppercase 'P'

All three cause errors. Java is case-sensitive throughout. It must be System.out.println — exactly.

4. Missing closing brace

Java
1public class HelloWorld { 2 public static void main(String[] args) { 3 System.out.println("Hello, World!"); 4 } 5// ← missing closing } for the class

Error: error: reached end of file while parsing

Fix: Count your { and }. Every opening brace needs a matching closing brace.

5. Running the wrong command

java HelloWorld.java // ← wrong, includes .java extension

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

Fix: Drop the extension — run java HelloWorld.

6. Forgetting to recompile after changes

You edit HelloWorld.java but run java HelloWorld without running javac HelloWorld.java first. The program runs the old .class file — your changes don't appear.

Fix: Every time you change the source file, recompile first with javac, then run with java.

What Each Part Is Called — Terminology

Learning the vocabulary now helps you read documentation and understand error messages:

PartTechnical NameExample
HelloWorld after classClass namepublic class HelloWorld
mainMethod name / Entry pointpublic static void main(...)
"Hello, World!"String literalThe text in quotes
System.out.println(...)Method call / StatementThe instruction that runs
;Statement terminatorEnds every statement
{ }Braces / BlockDefine the body of a class or method
//Comment// This is a comment — ignored by compiler

Add Your First Comment

Comments are lines the compiler completely ignores. They exist only for humans reading the code — to explain what it does or why.

Java
1// This is a single-line comment 2 3public class HelloWorld { 4 5 // This method is the entry point — the JVM starts here 6 public static void main(String[] args) { 7 8 // This prints a message to the terminal 9 System.out.println("Hello, World!"); 10 } 11}

Output is identical — comments don't affect what the program does. But they make your code readable to anyone (including yourself, six months later).

There's also a multi-line comment style:

Java
1/* 2 This is a multi-line comment. 3 It can span multiple lines. 4 The compiler ignores everything between /* and */ 5*/

The 3 Things to Remember From This Page

  1. Every Java program needs a class and a main method — the class is the container, main is where execution starts
  2. System.out.println() prints a line of text to the screen — this is one of the most-used lines in all of Java
  3. The filename must match the class name exactlyHelloWorld.java must contain public class HelloWorld. This is the single most common beginner mistake

Summary

  • Java programs are written inside a class — a named container for your code
  • The main method is the entry point — the JVM starts execution here
  • System.out.println() prints text and moves to the next line; System.out.print() prints without a newline
  • Compile with javac HelloWorld.java → run with java HelloWorld
  • Java is case-sensitiveSystem, out, and println must be capitalized exactly right
  • Every statement ends with a semicolon ;
  • Every opening { must have a matching closing }
  • Comments (// and /* */) are ignored by the compiler — use them to explain your code

What to Read Next

TopicLink
Java syntax rules and program structureSyntax & Structure →
Variables and data typesData Types →
How Java compiles and runs internallyHow Java Works →
Java vs other languages — key differencesJava vs Other Languages →
Getting user input with ScannerUser Input (Scanner) →
First Java Program | DevStackFlow