First Java Program (Hello World Program)

Introduction

Writing your first Java program is one of the most exciting milestones when learning Java. It helps you understand the basic structure of a Java application, how Java source code is compiled, and how it is executed by the Java Virtual Machine (JVM).

Traditionally, the first program every programmer writes is the “Hello World” program. Although it is simple, it introduces important Java concepts such as classes, methods, the main() method, statements, and the compilation process.

Before creating your first Java program, ensure that the Java Development Kit (JDK) is installed and configured correctly on your computer.

What is a First Java Program?

A First Java Program is a simple Java application that demonstrates the basic syntax and execution process of the Java programming language.

The most common first program prints the message:


Hello, World!

Although the program is small, it introduces the essential building blocks of every Java application.

Requirements Before Writing a Java Program

Before creating your first Java program, make sure you have:

  • Installed the Java Development Kit (JDK).
  • Configured the JAVA_HOME environment variable (if required).
  • Verified the installation using:
    • java -version
    • javac -version
  • Installed a code editor or Java IDE.

Creating Your First Java Program

Create a new file named:


HelloWorld.java

The filename should exactly match the public class name.

Add the following code:


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

Save the file.

Understanding the Program

Let’s understand each part of the program.

1. Class Declaration


public class HelloWorld

A class is a blueprint for creating objects.

Here:

  • public means the class is accessible from anywhere.
  • class is a keyword used to declare a class.
  • HelloWorld is the class name.

If the class is declared as public, the filename must be HelloWorld.java.

2. The main() Method


public static void main(String[] args)

This is the entry point of every standalone Java application.

Explanation:

  • public – accessible from anywhere.
  • static – belongs to the class rather than an object.
  • void – does not return any value.
  • main – the method where program execution begins.
  • String[] args – stores command-line arguments.

Without the main() method, the JVM cannot start the program.

3. Printing Output


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

This statement prints text to the console.

  • System is a built-in Java class.
  • out is the standard output stream.
  • println() prints the text and moves the cursor to the next line.

How to Compile a Java Program

Open Command Prompt (Windows) or Terminal (macOS/Linux).

Navigate to the folder containing the Java file.

Compile the program:


javac HelloWorld.java

If there are no errors, the Java compiler creates:


HelloWorld.class

This .class file contains Java bytecode.

How to Run the Program

Execute the following command:


java HelloWorld

Notice that you do not include the .class extension.

Output:

Hello, World!

Congratulations! You have successfully written and executed your first Java program.

Java Program Execution Process

The following steps describe how Java executes a program.


  Write Java Source Code (.java)
            │
            ▼
  Compile using javac
            │
            ▼
  Bytecode (.class)
            │
            ▼
  Load Bytecode into JVM
            │
            ▼
  JVM Converts Bytecode to Machine Code
            │
            ▼
     Program Output

This process allows Java applications to run on multiple operating systems.

Using an IDE

Instead of using the command line, you can use an IDE.

Popular Java IDEs include:

  • IntelliJ IDEA
  • Eclipse
  • Apache NetBeans
  • Visual Studio Code (with Java extensions)

Typical steps:

  1. Create a new Java project.
  2. Create a new Java class.
  3. Write the code.
  4. Click the Run button.

The IDE automatically compiles and executes the program.

Another Example

Let’s print multiple lines.


public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
        System.out.println("Learning Java is fun.");
        System.out.println("Happy Coding!");
    }
}

Output:

Welcome to Java!
Learning Java is fun.
Happy Coding!

Example Using Variables

Java programs often use variables to store data.


public class Student {
    public static void main(String[] args) {
        String name = "John";
        int age = 20;
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
    }
}

Output:

Name: John
Age: 20

Real-Life Example

Imagine you’re creating a welcome screen for a school management system.

When a student logs in, the application displays:


Welcome to ABC School
Student Portal
Have a Great Learning Experience!

Java code:


public class WelcomeScreen {
    public static void main(String[] args) {
        System.out.println("Welcome to ABC School");
        System.out.println("Student Portal");
        System.out.println("Have a Great Learning Experience!");
    }
}

This demonstrates how Java programs are used to display information and interact with users.

File Naming Rules

Java has strict file naming conventions.

Correct:


HelloWorld.java

Class:


public class HelloWorld

Incorrect:


hello.java

Class:


public class HelloWorld

The filename must exactly match the public class name, including letter casing.

Java Coding Best Practices

When writing Java programs:

  • Use meaningful class names.
  • Follow PascalCase for class names.
  • Keep one public class per file.
  • Indent code consistently.
  • Add comments where necessary.
  • Save files with the .java extension.
  • Organize your source files into packages as your projects grow.
  • Test your code after every change.

Common Mistakes

Beginners frequently encounter the following issues.

1. Incorrect File Name

Incorrect:


hello.java

Class:


public class HelloWorld

Correct:


HelloWorld.java

2. Missing Semicolon

Incorrect:


System.out.println("Hello")

Correct:


System.out.println("Hello");

3. Incorrect main() Method

Incorrect:


public void main()

Correct:


public static void main(String[] args)

4. Wrong Command to Run

Incorrect:


java HelloWorld.class

Correct:


java HelloWorld

5. Forgetting to Compile

Running:


java HelloWorld

before compiling:


javac HelloWorld.java

results in an error because the .class file does not yet exist.

6. Mismatched Braces

Incorrect:


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

Correct:


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

7. Typing Errors

Incorrect:


System.out.printlin("Hello");

Correct:


System.out.println("Hello");

Pay close attention to method names and capitalization.

Conclusion

Writing your first Java program is the foundation of your Java programming journey. The simple Hello World application introduces the essential concepts of Java, including classes, the main() method, statements, compilation, and execution. Although the program is small, it demonstrates the complete Java workflow—from writing source code and compiling it into bytecode to running it with the Java Virtual Machine (JVM).

Writing Your First Java Program – Interview Questions

Q 1: What is the extension of Java source file?
Ans: .java
Q 2: Why is the main method important?
Ans: It is the entry point of Java program execution.
Q 3: What does public static void main mean?
Ans: It allows JVM to call the method without creating an object.
Q 4: How do you display output in Java?
Ans: Using System.out.println().
Q 5: Can a Java program run without compilation?
Ans: No, Java code must be compiled before execution.

Writing Your First Java Program – Objective Questions (MCQs)

Q1. What is the entry point of a Java program?






Q2. Which method signature is correct for main()?






Q3. Which statement is used to print output on the console?






Q4. What does the Java compiler generate?






Q5. Which keyword is used to define a class in Java?






Related Java Tutorials