DEV Community

Cover image for Your First Java Program: A Step-by-Step Guide
Sharique Siddiqui
Sharique Siddiqui

Posted on • Edited on

Your First Java Program: A Step-by-Step Guide

Learning to write your very first Java program is a foundational milestone for every new programmer. No matter your background, following these clear steps will help you create, run, and understand a simple Java application—traditionally called “Hello, World!”

Prerequisites: What You Need

Java Development Kit (JDK): Already set up on your system. If not, follow my previous blog post about setting up the Java environment.

Text Editor or IDE: Use any text editor (Notepad, TextEdit) or an Integrated Development Environment (IDE) like Eclipse, IntelliJ IDEA, or VS Code.

Step 1: Write the Code

Open your chosen text editor or IDE and type in the following code:

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

A Few Key Points:

The filename must match the public class name. In this example, save the file as HelloWorld.java.

The main method (public static void main(String[] args)) is the entry point of any Java application.

System.out.println("Hello, World!"); prints the text to your screen or console window.

Step 2: Save the File

Save your code as HelloWorld.java in a folder you can easily access (e.g., Documents/Java).

Step 3: Compile the Program

To turn your Java code into something the computer can understand, you must compile it.

Open your terminal or command prompt.

Navigate to the folder containing your Java file, for example:

java

cd Documents/Java
Enter fullscreen mode Exit fullscreen mode

Compile your program with:

java

javac HelloWorld.java
Enter fullscreen mode Exit fullscreen mode

If there are no errors, this command will create a file named HelloWorld.class in the same directory.

Step 4: Run the Program

After compiling, run your program with:

java

java HelloWorld
Enter fullscreen mode Exit fullscreen mode

You should see:

java

Hello, World!
Enter fullscreen mode Exit fullscreen mode

displayed in your terminal or console—congratulations on writing your first Java program!

Understanding the Program

Let’s break down the essential components:

public class HelloWorld: Declares a class named HelloWorld.

public static void main(String[] args): The main method where the program begins.

System.out.println("Hello, World!");: A command to print the specified string to the console.

Troubleshooting Tips

Filename: Ensure your file is named HelloWorld.java (match uppercase/lowercase).

PATH Issues: If javacor java commands are not recognized, double-check your JDK installation and PATH environment variable.

Compilation Errors: Ensure all braces {} are matched and syntax matches the sample.

Final Thoughts

Writing and running your first Java program gets you comfortable with the Java development process—editing code, compiling, and executing. As simple as “Hello, World!” seems, it lays the groundwork for more complex and exciting Java projects ahead. Happy coding!

Check out the YouTube Playlist for great java developer content for basic to advanced topics.

Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud

Top comments (0)