Java Basics, Java Programming · December 2, 2026

Java for Beginners, Lesson 5: Hello World Program Explained Line by Line

Java for Beginners · Module 1: Getting Started


🎯 Why Study a Tiny Program So Closely?

The first program looks small, but it contains the skeleton of every Java program you will ever write. Learning it line by line is like learning the parts of a bicycle before riding: once you know them, nothing surprises you later.

📌 Every Java program needs a class, and every runnable program needs a method named main with exactly the right signature.

1️⃣ The Smallest Program

public class FirstProgram {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}
🖥️ Output:
Hello, World!
public class FirstProgram {
    public static void main(String[] args) {
        System.out.println(“Hello, World!”);
    }
}
PartMeaning
public class FirstProgramClass declaration: every Java program lives in a class. The file must be FirstProgram.java.
public static void main(String[] args)The main method: the JVM starts here.
System.out.println(“Hello, World!”);A statement: prints text, then a new line. It ends with a semicolon.
{ }Braces mark where the class and the method begin and end.

Figure 1: Anatomy of the smallest Java program.

2️⃣ The Class

  • class is the Java word for a blueprint. All Java code lives inside classes.
  • public means other code can see this class.
  • The class name is FirstProgram. By convention, class names start with a capital letter and use CamelCase: StudentCard, BankAccount.
  • The file name must be FirstProgram.java, the same as the public class.

3️⃣ The main Method

When you type java FirstProgram, the JVM searches the class for main and starts there. The signature must match exactly.

public
anyone can call it, so the JVM can
static
runs without creating an object first
void
returns nothing
main
the fixed name the JVM looks for
String[] args
words typed after the class name

Figure 2: The five words of the main method signature.

  • public: the JVM is outside your class, so it needs permission to call main.
  • static: the JVM has not created any object yet, so main must belong to the class itself.
  • void: main does not give a value back.
  • String[] args: an array of words typed after the class name on the command line (you used it in Lesson 2).

4️⃣ Printing with System.out

System
a class from the standard library
.
out
the standard output (your screen)
.
println
method: print text, then newline
(
"Hello"
what to print
);

Figure 3: System.out.println(“Hello”) taken apart.

  • System.out.println(x) prints x and then moves to a new line.
  • System.out.print(x) prints x and stays on the same line.
  • System.out.printf(format, values...) prints with a format, as in C. Use %d for int, %f for decimals, %s for text and %n for a new line.

Program 2: print, println, printf and escape characters

public class PrintStyles {
    public static void main(String[] args) {
        System.out.print("One ");
        System.out.print("Two ");
        System.out.println("Three");
        System.out.println("Line A");
        System.out.println("Name:\tAsha");
        System.out.println("Quote: \"Java\" is fun");
        System.out.println("Path: C:\\JavaPrograms");
        System.out.printf("Pi is about %.3f%n", Math.PI);
        System.out.println();
        System.out.println("After a blank line");
    }
}
🖥️ Output:
One Two Three
Line A
Name: Asha
Quote: “Java” is fun
Path: C:\JavaPrograms
Pi is about 3.142

After a blank line
  • The first three lines use print twice and println once, so “One Two Three” appears on one line.
  • \t prints a tab, so there is a wide gap after “Name:”.
  • To print a double quote inside text, write \". To print a backslash, write \\.
  • %.3f prints three digits after the decimal point, so pi shows as 3.142.
  • System.out.println() with nothing inside prints just an empty line.
EscapeMeaningExamplePrints
\nnew line“A\nB”A then B on the next line
\ttab“A\tB”A, a wide space, B
\”double quote“Say \”Hi\””Say “Hi”
\\backslash“C:\\Java”C:\Java
%nnew line inside printfprintf(“A%n”)A and a new line

Figure 4: Common special characters inside Java text.

5️⃣ More Than One Class in a File

A file may contain several classes, but only one of them can be public, and the file name follows that class.

Program 3: A helper class (saved as Greeter.java)

class Helper {
    static void greet(String name) {
        System.out.println("Hello, " + name + "!");
    }
}

public class Greeter {
    public static void main(String[] args) {
        Helper.greet("Asha");
        Helper.greet("Ravi");
    }
}
🖥️ Output:
Hello, Asha!
Hello, Ravi!
  • Helper is not public, so it can share the file with Greeter.
  • Helper.greet("Asha") calls the static method through the class name. greet takes text and prints a greeting.
  • Run the program with java Greeter, because Greeter has the main method.

6️⃣ A Small Useful Program

public class StudentCard {
    public static void main(String[] args) {
        String name = "Asha Verma";
        int roll = 21;
        double cgpa = 8.75;

        System.out.println("=== Student Card ===");
        System.out.printf("Name : %s%n", name);
        System.out.printf("Roll : %d%n", roll);
        System.out.printf("CGPA : %.2f%n", cgpa);
        System.out.println("====================");
    }
}
🖥️ Output:
=== Student Card ===
Name : Asha Verma
Roll : 21
CGPA : 8.75
====================
  • The program stores three values in variables of types String, int and double. You will study variables properly in Lesson 7.
  • %s, %d and %.2f are placeholders that printf fills with the variables, in order.

7️⃣ What the Compiler and JVM Say When Something Is Wrong

No static in main

public class NoStatic {
    public void main(String[] args) {
        System.out.println("Hello");
    }
}
🖥️ Message from java:
Error: Main method is not static in class NoStatic, please define the main method as:
public static void main(String[] args)

Wrong capital letter in main

public class WrongName {
    public static void Main(String[] args) {
        System.out.println("Hello");
    }
}
🖥️ Message from java:
Error: Main method not found in class WrongName, please define the main method as:
public static void main(String[] args)
or a JavaFX application class must extend javafx.application.Application

A missing closing brace

public class Unbalanced {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
🖥️ Compiler message:
Unbalanced.java:4: error: reached end of file while parsing
}
^
1 error
  • Both main mistakes compile fine, but java cannot start the program. These are runtime errors, not compile errors.
  • “Reached end of file while parsing” almost always means a { was never closed. Count your braces.

⚠️ Common Mistakes

MistakeProblemFix
public static void MainJava looks for lowercase mainWrite main
Leaving out staticThe JVM cannot call itUse public static void main
Missing ;Statement is not finishedEnd every statement with ;
System.out.println("Hi')Mixed quote typesText uses double quotes
Unbalanced braces“reached end of file”Match every { with a }
Class name and file name differCompile errorMake them identical

❓ Frequently Asked Questions

Q1. Why is the program inside a class even for tiny programs?
Java is an object-oriented language, so all code must belong to a class.

Q2. Is String[] args required?
Yes in this form. You can rename args, but the type must stay String[].

Q3. What is the difference between println and printf?
println prints a value and a new line. printf uses a format string, and you must add %n yourself for a new line.

Q4. Can I have two main methods?
One per class is normal. You choose which class to run with java ClassName.

✅ Key Takeaways

  • A Java program is a class with a public static void main(String[] args) method.
  • The file name must match the public class name.
  • print stays on the line, println ends it, and printf formats.
  • Use \n, \t, \" and \\ for special characters in text.
  • Statements end with ; and blocks use matching { }.
  • Java is case-sensitive: Main is not main, and system is not System.
🎓 Practice questions: (1) Explain each word in public static void main(String[] args). (2) Write a program that prints your name, branch and college on three lines. (3) Print the text He said "Hi" using escape characters. (4) What is the difference between a compile error and the “Main method not found” error? (5) Use printf to print the number 3.14159 with two decimal places.
All 30 lessons in this series (open course page)

Module 1 · Getting Started

Module 2 · Building Blocks

Module 3 · Operators

Module 4 · Control Flow

Module 5 · Data and Methods

Module 6 · Objects and Revision