How to create a hello world program in Java

In this article we will learn a simple java program to output “Hello World” in the console.

To run your first Java program, 

  • you must have JDK installed on your PC
  • You must set path  JDK/bin

To write a simple HelloWorld program

  • Create a file named HelloWorld.java
  • Write the code as below
  • Compile and Run

Code:

/**
 * This program prints "Hello World!" in the console.
 */
class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World");
    }
}

Output:

Hello World

Class definition:

class is a keyword and HelloWorld is the class name. Below is the class definition in HelloWorld.java.

class HelloWorld {
    ---
    ---
    ---
}

Main method:

Every Java application must have a main method. Below is the main method in HelloWorld.java

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

Print Statement:

System is a Class out is a Variable println() is a method. below is the print statement which will print the “Hello World” in the console.

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

Code comments:

Code comments will be ignored by the compiler. And these are very useful to other programmers. Below are the code comments in HelloWorld.java

/** * This program prints "Hello World!" in the console. */