Java Hello World Program

Introduction

The Java Hello World Program is the first program that every Java beginner learns. It introduces the basic structure of a Java program and demonstrates how to display output on the console.

Writing a Hello World program helps you verify that your Java Development Kit (JDK) is installed correctly and that your development environment is ready for Java programming.

Java Hello World Program


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

Output

Hello, World!

Understanding the Program

Let’s understand each line of the program.

1. Class Declaration


public class HelloWorld
  • public is an access modifier.
  • class is the keyword used to declare a class.
  • HelloWorld is the class name.
  • In Java, every program must be inside a class.

2. Main Method


public static void main(String[] args)

This is the entry point of every Java application.

Explanation:

  • public – Accessible from anywhere.
  • static – Can be called without creating an object.
  • void – Does not return any value.
  • main – The method where program execution starts.
  • String[] args – Stores command-line arguments.

3. Printing Output


System.out.println("Hello, World!");
  • System is a predefined Java class.
  • out is the standard output stream.
  • println() prints text and moves the cursor to the next line.

How to Compile and Run the Program?

Step 1: Save the Program

Save the file as:


HelloWorld.java

Note: The filename must exactly match the public class name.

Step 2: Compile

Open the command prompt or terminal and run:


javac HelloWorld.java

If there are no errors, Java creates:


HelloWorld.class

This file contains Java Bytecode.

Step 3: Run

Execute the program using:


java HelloWorld

Output

Hello, World!

Program Flow

The execution follows these steps:

  1. Write the Java source code.
  2. Save it as HelloWorld.java.
  3. Compile it using javac.
  4. The compiler generates HelloWorld.class.
  5. The JVM executes the bytecode.
  6. The message Hello, World! is displayed.

Java Program Structure

A basic Java program consists of:

  • Package declaration (optional)
  • Import statements (optional)
  • Class declaration
  • Main method
  • Statements inside the main method

Example:


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

Another Example:

Printing multiple lines:


public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Welcome to Java");
        System.out.println("This is my first Java program");
    }
}

Output

Welcome to Java
This is my first Java program

Using print() Instead of println()


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

Output

Hello World

Difference:

  • print() prints text on the same line.
  • println() prints text and then moves to the next line.

Common Errors

1. Incorrect File Name

Incorrect:


hello.java

Correct:


HelloWorld.java

The filename must match the public class name.

2. Missing Semicolon

Incorrect:


System.out.println("Hello")

Correct:


System.out.println("Hello");

3. Misspelling main

Incorrect:


public static void Main(String[] args)

Correct:


public static void main(String[] args)

Java is case-sensitive.

4. Forgetting Quotes

Incorrect:


System.out.println(Hello World);

Correct:


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

5. Missing Curly Braces

Always ensure that every opening brace { has a corresponding closing brace }.

Best Practices

  • Use meaningful class names.
  • Match the filename with the public class name.
  • Follow Java naming conventions.
  • Indent your code properly.
  • Use println() for readable output.
  • Keep the main() method simple for beginner programs.

Summary

The Java Hello World program is the foundation of Java programming. It teaches the basic syntax of a Java class, the main() method, and how to display output using System.out.println(). Understanding this simple program is the first step toward learning more advanced Java concepts such as variables, loops, methods, object-oriented programming, and the Collections Framework.