Learnmonkey Learnmonkey Logo
× Warning:

The tutorial you are reading is currently a work in-progress.

Your First Java Program

Writing the Code

Below is the code we will write:


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

Save the code in a file called HelloWorld.java.

Running the Code

First of all, you open the command line and navigate to the folder where you stored the code file.

Then, run the following command in the command line, which compiles the Java code into bytecode:

javac HelloWorld.java

After you run the command, you should see a new file called HelloWorld.class, which stores the bytecode.

Then, run the following command, which interprets the bytecode:

java HelloWorld

Once you run the code, you should see:

Hello World!

Code Breakdown

All Java programs have a class definition. The name of the class is the same as the name of the Java file. We will learn about classes in a later tutorial:

class HelloWorld {
        ...
    }

Inside of the class, we have the main function, which we will learn more about in a later tutorial. Just know that when Java runs the file, the code inside the main function will be run:

public static void main(String[] args) {
        ...
    }

Finally, inside of the main function, we output Hello, World!:

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