Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Hello World Program
Your First Step Into Java



Writing your First Program in Java

Writing your first program in Java is a rite of passage into the world of programming. It's a small step with big implications — it confirms your environment is set up and gives you a working program to tweak and explore. Let's start your Java journey with the most iconic program: Hello, World!

Step 1: Verifying if Java is Installed

Before we dive into the code, make sure you have the Java Development Kit (JDK) installed:

Open a Command Prompt or Terminal and run the following command.

java -version

You should see an output like:

java version "17.0.2"  
Java(TM) SE Runtime Environment ...

If not, go through the previous topics in this course where we have provided instructions to install Java in Linux, Windows 10/11, and macOS.

Step 2: Writing Your First Java Program

Create a file named HelloWorld.java. Java is strict about naming — your filename must match the class name if it's public. HelloWorld in HelloWorld.java should be same as HelloWorld in public class HelloWorld.

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

Step 3: Understanding the Code

Step 4: Compiling the Program

Open a terminal or command prompt where your HelloWorld.java file is saved and run the following command:

javac HelloWorld.java

What happens during compilation?

The javac command invokes the Java compiler. Here's what it does behind the scenes:

This bytecode is what the Java Virtual Machine (JVM) understands and executes — not the original source code.

Step 5: Running the Program

Now, execute the compiled bytecode using the java command:

java HelloWorld

What happens during execution?

The java command calls the JVM (Java Virtual Machine) to:

Hello, World!

You’ve just compiled and executed your first Java program. This process — compile once, run anywhere — is what makes Java so powerful and portable.

Common Issues to Watch For



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M