First Java Program in Java (Hello World) – Beginner's Guide (2026)

Learn how to write, compile, and run your first Java program. A beginner-friendly guide with code examples, explanations, common mistakes, and best practices.

First Java Program in Java

First Java Program in Java

Writing your first Java program is the moment where everything you've learned so far comes together. After installing the Java Development Kit (JDK) and an IDE like IntelliJ IDEA, you're ready to write your first line of Java code.

In this guide, you'll create the classic "Hello, World!" program, understand every line of code, compile it, and run it successfully.


Quick Answer

Your first Java program usually looks like this:

public class HelloWorld {

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

}

Output:

Hello, World!

Although this program prints only one line, it introduces the most important parts of every Java application.


Prerequisites

Before writing your first Java program, make sure you've completed these steps:

java -version

and

javac -version

If both commands work, you're ready to start coding.


Create Your First Java Program

We'll use IntelliJ IDEA because it's one of the most popular Java IDEs.

Step 1: Open IntelliJ IDEA

Launch IntelliJ IDEA.

Click:

New Project


Step 2: Select Java

Choose:

  • Java

  • Select your installed JDK

  • Click Next


Step 3: Create a Class

Right-click the src folder.

Choose :

New → Java Class

Name it :

HelloWorld

Press Enter.


Write Your First Java Code

Replace the generated code with:

public class HelloWorld {

    public static void main(String[] args) {

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

    }

}

Save the file.


Run the Program

Click the green ▶ button beside the main() method.

Or press :

Shift + F10

Output

Hello, World!

Congratulations!

You have successfully executed your first Java program.


Understanding Every Line

Let's understand what's happening.

1. Class Declaration

public class HelloWorld

A class is a blueprint that contains Java code.

Every Java program must be inside a class.

Here,

HelloWorld

is the class name.

Remember:

The file name must match the class name.

Correct :

HelloWorld.java

Incorrect :

Program.java

2. Main Method

public static void main(String[] args)

This is the starting point of every Java application.

When you run a Java program, the JVM looks for this method first.

Without it, Java doesn't know where to begin executing your code.


3. Printing Output

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

This statement prints text to the console.

println() means:

Print the text and move to the next line.

Example:

System.out.println("Java");
System.out.println("Programming");

Output:

Java
Programming

Using print() Instead

System.out.print("Java ");
System.out.print("Programming");

Output:

Java Programming

Unlike println(), the print() method does not move to the next line.


Printing Multiple Lines

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("Welcome");
        System.out.println("to");
        System.out.println("TheTechLane");

    }

}

Output

Welcome
to
TheTechLane

Printing Numbers

Java can also print numbers.

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println(100);

        System.out.println(250 + 350);

    }

}

Output

100
600

Printing Variables

public class HelloWorld {

    public static void main(String[] args) {

        String name = "mohit";

        System.out.println(name);

    }

}

Output

mohit

You'll learn variables in detail later.


Compile and Run from Command Prompt

You don't always need an IDE.

You can also use the command line.

Navigate to your project folder.

Compile :

javac HelloWorld.java

If compilation succeeds, Java creates:

HelloWorld.class

Now run it :

java HelloWorld

Output:

Hello, World!

How Java Executes Your Program

HelloWorld.java
        │
        ▼
     javac
        │
        ▼
HelloWorld.class
        │
        ▼
      JVM
        │
        ▼
 Console Output

This process happens every time you run a Java application.



Best Practices

  • Use meaningful class names.

  • Keep one public class per file.

  • Follow Java naming conventions.

  • Write clean and readable code.

  • Save your file before running it.

  • Practice by making small changes to the program.


Try It Yourself

Modify the program to print your own information.

public class HelloWorld {

    public static void main(String[] args) {

        System.out.println("My Name is Alex");
        System.out.println("I am learning Java");
        System.out.println("Welcome to TheTechLane");

    }

}

Try changing the text and run the program again.

Experimenting is one of the fastest ways to learn.


Interview Questions

Why is the main() method important?

It is the entry point of a Java application.

Can a Java program run without a class?

No. Every Java program must be inside a class.

What is the difference between print() and println()?

print() prints on the same line, while println() prints the text and moves the cursor to the next line.

What does javac do?

It compiles Java source code into bytecode (.class files).

What does the JVM do?

The JVM executes the compiled bytecode and allows Java programs to run on different operating systems.


Frequently Asked Questions

Is "Hello, World!" mandatory?

No. It's simply the traditional first program used to verify that your environment is working correctly.

Can I use Notepad instead of IntelliJ IDEA?

Yes. Any text editor can be used, but an IDE makes development much easier.

Why is Java case-sensitive?

Java treats uppercase and lowercase letters as different characters. For example, Main and main are not the same.

What happens if I remove the main() method?

The program will compile, but it won't run as a standalone application because the JVM won't find an entry point.

Can I print multiple lines?

Yes. Use multiple System.out.println() statements or include newline characters (\n) within a string.


Conclusion

Congratulations! You've written and executed your first Java program.

While it may seem simple, this small program introduces the core building blocks of every Java application: classes, the main() method, and console output. Understanding these concepts now will make learning variables, data types, methods, and object-oriented programming much easier.

As you continue learning, don't just copy code modify it, experiment with it, and observe what changes. That's how you'll build confidence as a Java developer.


Key Takeaways

  • You wrote and successfully ran your first Java program.

  • Every standalone Java application starts execution from the main() method.

  • System.out.println() is used to display output in the console.

  • Java source code (.java) is compiled into bytecode (.class) using the javac compiler.

  • The Java Virtual Machine (JVM) executes the compiled bytecode.

  • Understanding the first program lays the foundation for learning Java syntax and programming concepts.


Previous Lesson

← Installing IntelliJ IDEA


What's Next?

Now that you've written your first Java program, it's time to understand what each part of the code does and why it's required.

Next Lesson → Java Program Structure


Continue Learning

Explore more beginner-friendly Java tutorials:


Have a Question?

If you run into any issues while writing or running your first Java program, leave a comment below.

We'll help you troubleshoot the problem with clear explanations and practical solutions so you can continue your Java learning journey with confidence.

Happy Coding! ☕


Follow TheTechLane

Want more Java tutorials, programming tips, and developer resources?

Follow TheTechLane on LinkedIn, X, Instagram, Threads, Facebook, Medium, and Pinterest to stay updated whenever a new tutorial is published.

Learn. Build. Share.

— TheTechLane