Java Hello World program

In this tutorial, we will write a java hello world program, compile, and execute it. Walk through on step by step from writing to till the execution of the program.

Hello World Program

To compile and execute any java programs requires a JDK installation on the local machine, if its available on the machine ignore this otherwise follow steps mentioned on Java installation on Windows to install JDK on local machine.

HelloWorld class

In Java, all programs are written under the class only, to write a program need to write a class as described below.

public class HelloWorld{
}

We have a class with the name HelloWorld, we can give any class name but should be suitable for the requirement for readability purpose on later stages.

HelloWorld main method

All Java applications will start the execution from the main method, as described below.

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

While executing the java application, JVM looks for the main method from main class and starts execution from there. All application should have a main method and syntax will be same for all as mentioned above.

Now, we need to print a line "Hello Java World!!!" and this can be printed with the statement as described below.

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

Combining of class, main method and print statement will be looks like below.

CopiedCopy Code
public class HelloWorld {
public static void main(String args[]) {
//Will print Hello Java World!!!
System.out.println("Hello Java World!!!");
}
}

Now you can execute the above program directly by pressing Run or perform the following steps to compile and execute locally.

HelloWorld program steps to compile and run:

Step 1:

Open the notepad in the windows machine or relevant text editor in other operating systems.

Step 2:

Copy the above example and paste it into the notepad or in text editor.

Step 3:

Save the text file with the name as SimpleProgram and file extension as java. Verify saved file will have the name as SimpleProgram.java

Step 4:

Execute the following command, in the location where SimpleProgram.java saved.

javac SimpleProgram.java

Step 5:

You will see a new file gets created in the same location where SimpleProgram.java saved. Newly created file will have the name as SimpleProgram.class, this is a compiled file of the SimpleProgram.java file.

Step 6:

To run the compiled java file, please execute the following command.

java SimpleProgram

Above command generates the following output.

Hello Java World!!!

Conclusion

In this tutorial, we have learned about compilation, execution step by step with the help of simple java program.