Write your first Java program

This tuorial introduces you to the basics of creating a very simple Java program. We will start with a traditional “Hello World!” program which simply displays the “Hello World!” text to the command line.

Step 1: Writing the Java Code

Open your favorite text editor(Notepad, Notepad++, visual studio code, etc.) and create a new file and add the below code:

class HelloWorld
    public static void main (String[] args)
            // Print Hello World! to the console
            System.out.println("Hello World!");

Some Highlights about the above code:

“//” – To write a comments in a single line

System.out.println() – This function writes data to the terminal.

main() – It is a method, which is always the entry point of a Java program, and the body is defined within enclosing curly braces. This method takes a String argument and is a public method and therefore available to anyone

Step 2: Save the file

Save your source code file as “HelloWorld.java”. You can save with any other name instead of “HelloWorld” but extension should be “.java”.

Step 3: Open a New Command Prompt

Open a new command prompt by running “cmd” in “Run” dialogue box and navigate to the path where the program code is saved.

Open Command Prompt

Step 4: Compile and Run the Java Code

We’re now ready to compile the program. Execute the below command.

javac HelloWorld.java

Java compiler will look for the “HelloWorld.java” file and compile the code and creates a class file “HelloWorld.class”. It contains an intermediate language called “ByteCodes”.

Run the below command to tun the program.

java HelloWorld

When we press Enter, the program runs and you will see “Hello World!”

written to the terminal window.


Congratulation. You’ve written your very first Java program.