Introduction

Java Program to Print Hello World

A simple Java program to print "Hello, World!" to the console.

Problem Description

Write a Java program that prints the message "Hello, World!" to the standard output (console).

Code

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

Output

Hello, World!

Explanation

  1. public class HelloWorld: Defines a class named HelloWorld. In Java, every application must contain a main class.
  2. public static void main(String[] args): This is the entry point of the program. The Java Virtual Machine (JVM) starts executing the program from this method.
  3. System.out.println(...): This statement prints the argument passed to it (in this case, "Hello, World!") followed by a new line to the console.

Try Yourself

Practice these variations to test your understanding.

  1. Print Your Name: Modify the program to print "Hello, [Your Name]!".
  2. Multiple Lines: Print "Hello" on the first line and "World" on the second line using two System.out.println statements.
  3. Same Line: Print "Hello World" using two System.out.print statements (notice the missing 'ln').
  4. Pattern Printing: Print a square of asterisks (*) using 3 print statements.
    ***
    ***
    ***
  5. Brain Twister: What happens if you remove static from the main method? Try it and see the error.