Simple Hello World Program in Java with Example

The basic building block of Java is class, so java program nothing but a Class. The class can contain methods, attributes, procedures. In a conventional language, we can refer to methods as functions and attributes as variables.

Hello World Program in Java, Simple Java Program, Java Hello World

Understanding the First Java Hello World Program

In these, all examples we have used many terms lets understand all one by one-

class – class keyword is used to marking that code as a class or is used to create/ declare a class.

public - the public is an access modifier which represents visibility, public means it is visible to all other classes.

static – is a keyword, if we declare any method as static, it is known as a static method that means there is no need to create an object to call this method. The static method can be called by class name. The main method is invoked by JVM, so it doesn’t need to create the object to call the main method. It saves memory.

void – void is a return type of the method. When a method is not returning any value that means it is returning void. It is mandatory to declare the return type void, we can not write a method without return type.

main – it is the name of the method. JVM always calls the method, we can not change the method name.
String[] args – It is used to accept the arguments from the command line to the program.

System.out.println() - it is used to print on console. Here System is class, out is the object of PrintStream class in System class and println() is a method of PrintStream class.

In the case of public class, the name o the class and name of the file must be the same. If the class is not public, it can be stored in any .java file. There can be only one public class in a .java file but multiple classes can be there in a single .java file.

The Requirement for Java Hello World Program

  • Install the JDK if you don't have installed it, download the JDK and install it.

  • Create the java program

  • Compile and run the java program

Variables contain values of data type int, float, double, Boolean, char, String. Methods perform operations. An executable class must have a default method ‘main’ that is called by JVM when we execute the class.
Create a first Java Program that will print ‘Hello World’ at the console.

Follow the steps to create the first Java program –

Step 1: create a folder c:\firstprogram

Step 2: create a text file inside c:\ firstprogram folder and write following code –

public class FirstHello{
public static void main(String args[]){
	System.out.println("Hello World");
Example Output

Step 3: Go to folder c:\ firstprogram and run following command to compile the code.
It will create c:\firstprogram\FirstHello.class file.

Step 4: Go to folder c:\firstprogram and run following command to execute code-
c:\firstprogram\java FirstHello

Step 5: If you are able to see the following output then congratulations you have successfully executed the first Java program.

simple hello world program in java Output

Test a few more code inside the main method -

1. Define a string and run the same program

public class FirstHello {
	public static void main(String args[]) {
		String name = "Java Point Tutorial";
		System.out.println("This is " + name);
Example Output

2. Print “Have a nice day” five times.

public class FirstHello {
	public static void main(String args[]) {
		int i = 0;
		while (i < 5)
			System.out.println("Have a nice day");
Example Output

3. Java Program Run-time to Addition Two Numbers

import java.util.Scanner;

public class Main{

	public static void main(String[] args) {

		Scanner s = new Scanner(;
		System.out.print("Enter first number: ");
		int number1 = s.nextInt();

		System.out.print("Enter second number: ");
		int number2 = s.nextInt();

		int add = number1 + number2;

		System.out.println("Output: " + add);
Example Output

Note: Run Program

  • javac
  • Java Main
  • Enter the First Numer: 5
  • Enter the Second Number: 5
  • Then Enter