Java First Program (Hello World)

Writing the first Java program is a rite of passage for every learner. The classic "Hello, World!" program is simple — it displays a message on the screen. But even this short program teaches the fundamental structure of every Java application.

The Hello World Program

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

When this program runs, it displays the following output:

Hello, World!

Understanding Every Line

Every part of this program has a specific role. Let's break it down step by step.

Line 1: public class HelloWorld

public class HelloWorld {
  • public: This is an access modifier. It means this class is accessible from anywhere.
  • class: This keyword is used to define a class. In Java, every program must have at least one class.
  • HelloWorld: This is the name of the class. The class name must exactly match the file name. Since the file is named HelloWorld.java, the class is also named HelloWorld.
  • { } (curly braces): These define the start and end of the class body — everything inside belongs to this class.

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

public static void main(String[] args) {

This is the main method. Every Java program must have this exact method — it is the entry point from where the program starts running.

  • public: The method is accessible from anywhere, including the JVM.
  • static: The method belongs to the class itself, not to any specific object. The JVM calls it without creating an object first.
  • void: This method does not return any value after it finishes.
  • main: This is the name of the method. The JVM looks specifically for a method called main to start the program.
  • String[] args: This allows the program to accept input from the command line when it starts. It is not used in this example, but it must still be there.

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

System.out.println("Hello, World!");
  • System: A built-in Java class that provides access to system resources.
  • out: A part of the System class that represents the output stream (the screen).
  • println: Short for "print line." It prints the given text to the screen and moves the cursor to the next line.
  • "Hello, World!": The text inside double quotes is called a String — a sequence of characters to be printed.
  • ; (semicolon): Every statement in Java must end with a semicolon.

How to Create and Run This Program

Using the Command Line

  1. Open a text editor (Notepad, gedit, or any editor).
  2. Type or paste the Hello World code.
  3. Save the file as HelloWorld.java (the file name must match the class name exactly, including capitalization).
  4. Open the terminal or command prompt.
  5. Navigate to the folder where the file is saved using the cd command:
    cd path/to/folder
  6. Compile the program:
    javac HelloWorld.java
    This creates a HelloWorld.class file containing the bytecode.
  7. Run the program:
    java HelloWorld
  8. The output appears:
    Hello, World!

Using an IDE (IntelliJ IDEA)

  1. Open IntelliJ IDEA and create a new Java project.
  2. Inside the project, create a new Java class named HelloWorld.
  3. Type the Hello World code into the class.
  4. Click the green Run button or press Shift + F10.
  5. The output appears in the console at the bottom of the screen.

Common Mistakes to Avoid

MistakeEffectCorrect Form
File name does not match class nameCompilation errorHelloWorld.javapublic class HelloWorld
Missing semicolon at end of statementCompilation errorSystem.out.println("Hello");
Wrong capitalization in System, StringCompilation errorSystem.out.println (capital S)
Missing curly bracesCompilation errorEvery opening { must have a matching closing }

print vs println

Java provides two methods for printing output:

  • System.out.println(): Prints the text and moves to the next line.
  • System.out.print(): Prints the text and stays on the same line.
public class PrintExample {
    public static void main(String[] args) {
        System.out.print("Hello ");
        System.out.print("World");
        System.out.println();
        System.out.println("New Line starts here");
    }
}

Output:

Hello World
New Line starts here

Summary

  • Every Java program must have a class and a main method.
  • The file name must match the public class name exactly.
  • The main method is where program execution begins.
  • System.out.println() is used to print output to the screen.
  • Every statement in Java ends with a semicolon ;.
  • Java is case-sensitive — Main and main are treated as different names.

Leave a Comment

Your email address will not be published. Required fields are marked *