Home› Java Programming Language› / Programs / My First Program in Java: Hello World
🚀 Programming Example

My First Program in Java: Hello World

43,878 Views
Java Java 8 or later
Beginner
Full Solution

Write your first Java program, Hello World. This beginner program prints "Hello World" using System.out.println() and explains the class, the main() method, and how to compile with javac and run with java.

Before You Begin

What you will learn

  • Write, compile and run your first Java program
  • Understand the basic structure of a Java program: class, main() method and statements
  • Know why the file name must match the public class name
  • Understand the purpose of each keyword in public static void main(String[] args)
  • Print text using System.out.println()
  • Understand how javac creates bytecode and the JVM runs it

Prerequisites

  • JDK (Java Development Kit) installed on your computer
  • A text editor or IDE such as Notepad, VS Code, Eclipse or IntelliJ IDEA
  • Basic use of the command prompt or terminal
  • The PATH variable set so that javac and java commands work

📌 Information & Algorithm

Algorithm

  1. Start
  2. Create a public class named HelloWorld.
  3. Define the main() method inside the class.
  4. Display the text "Hello World" using System.out.println().
  5. Stop

How to Run

Environment: JDK (Windows, Linux, macOS)

Execution command:

javac HelloWorld.java
java HelloWorld

💻 Program Code

// My first program in Java
public class HelloWorld {

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

Sample Input

No input required

🖥 Program Output

                                Hello World
                            

📘 Explanation

Overview

The Hello World program is traditionally the first program you write in any language. It prints one line of text and shows the minimum structure every Java program needs: a class, a main() method and a statement.

Save this file as HelloWorld.java. The file name must exactly match the public class name, including capital letters.

Step-by-Step Explanation

1

Class Declaration

Every Java program lives inside a class.

public class HelloWorld {
    ...
}

public is an access modifier, class is the keyword that defines a class, and HelloWorld is its name. The curly braces { } mark where the class starts and ends.

2

The main() Method

The entry point of the program.

public static void main(String[] args)

When you run the program, the JVM (Java Virtual Machine) looks for this exact method and starts running from here.

3

Print Statement

Displays text on the screen.

System.out.println("Hello World");

System is a built-in class, out is the standard output stream (the console), and println() prints the text and moves to a new line. Text in double quotes is a String, and the statement ends with a semicolon ;.

Parts of the main() Method

Keyword Meaning
public The JVM can call this method from outside the class.
static The method runs without creating an object of the class.
void The method doesn't return a value.
main The method name the JVM looks for to start the program.
String[] args Holds command-line arguments, if any are passed.

How a Java Program Runs

COMPILE AND RUN
HelloWorld.java → javac → HelloWorld.class → JVM → Output

javac compiles your source code into bytecode (HelloWorld.class). The java command then runs that bytecode on the JVM. That's why the same .class file runs on Windows, Linux and macOS.

javac HelloWorld.java
java HelloWorld

Output

Hello World

Common Mistakes

Missing the class keyword Writing HelloWorld { without public class gives a compile error such as class, interface, enum, or record expected.
File name doesn't match the class name A public class named HelloWorld must be saved in HelloWorld.java, not helloworld.java or Main.java.
Wrong capitalisation Java is case-sensitive. system.out.println or Main won't work.
Adding .class when running java HelloWorld.class fails. Run it with java HelloWorld.
Tip for Java 11 and later You can skip the compile step for a single file and run java HelloWorld.java directly.

Key Takeaway

Every Java program needs a class and a main() method. Compile with javac, run with java, and print output with System.out.println().

Complexity

Time: O(1)

Space: O(1)

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.