Writing and Running Your First Java Program

7/19/2025

diagram for Writing and Running Your First Java Program

Go Back

Writing and Running Your First Java Program: A Beginner’s Guide

Java is one of the most popular programming languages in the world. It's known for its portability, object-oriented structure, and wide usage in enterprise applications. If you're just getting started, writing your first Java program is a great step toward becoming a developer.

This guide will walk you through installing Java, writing a simple program, and running it.


 diagram for Writing and Running Your First Java Program

Step 1: Install Java Development Kit (JDK)

Before you can write Java code, you need the Java Development Kit (JDK) installed.

 How to Install JDK:

  1. Go to https://www.oracle.com/java/technologies/javase-downloads.html

  2. Download and install the latest JDK version for your operating system.

  3. Verify installation:

java -version
javac -version

Step 2: Set Up Your Development Environment

You can use any text editor or an Integrated Development Environment (IDE) like:

  • VS Code

  • IntelliJ IDEA

  • Eclipse

For beginners, VS Code or IntelliJ IDEA is recommended.


Step 3: Write Your First Java Program

Open your editor and create a file named HelloWorld.java. Then, type the following code:

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

 Explanation:

  • public class HelloWorld: Defines a class named HelloWorld.

  • main(String[] args): The entry point of any Java application.

  • System.out.println: Prints text to the console.


Step 4: Compile the Java Program

Use the terminal or command prompt to compile the file:

javac HelloWorld.java

If there are no errors, it creates a HelloWorld.class file (bytecode).


Step 5: Run the Java Program

Execute the program with:

java HelloWorld

Output:

Hello, World!

Bonus Tip: Common Mistakes to Avoid

  • File name and class name should match.

  • Java is case-sensitive.

  • Don't forget the semicolon at the end of statements.


What’s Next?

Now that you've written your first Java program, explore:

  • Variables and data types

  • Control flow (if, switch, loops)

  • Object-Oriented Programming (OOP) basics

  • Java packages and modules


Conclusion

Writing and running your first Java program is the foundation of your programming journey. Java is a robust, scalable language that powers everything from Android apps to enterprise systems.

Keep practicing, and soon you'll be building more advanced applications with confidence!

Table of content