Java Program to Print Hello World

In this Java tutorial, you will learn how to write your first program in Java to print Hello World.

Java is a programming language. It is based on an object-oriented programming language model. In JAVA everything is defined as an object and JAVA is a case-sensitive language.  

How to Print Hello World in Java?

Let’s start the Hello world program: 

RUN CODE SNIPPET
public class Main
{
   public static void main(String[] args)
  {
     System.out.println("Hello world!"); 
  }
}

Output: 

Hello world!

Structure of Hello World program  

  • Class  
  • Main method 

class 

Line 1: public class Main  

Here we write our code in a class called main, the public means that anyone can access it and in java, every code line should be inside a class.  

Main method  

The main method in JAVA is always static. It is the entry point of our program, where the compiler starts to execute the program. 

Line 2: public static void main(String[] args)   

Public– The public access modifier shows the visibility of the program. It means it is visible to everyone.  

Static– The static keyword is a class level and used for memory management. It means no need to create a class.  

Main– The main represents the startup of the program, where the execution of the program starts.  

String[] args– The command line arguments or the array of a sequence of characters that are passed to the main function.

Line 3: System.out.println(“Hello world!”);  

This line is used to print the statement. 

System– The System is a pre-defined class, and it holds some variables and methods.  

Out– Out is an instance of Printstream type and it is a static variable.  

Println– Any statement given inside the println command will be printed.  

Note: 

The class definition should be inside the main method. 

JAVA is a case-sensitive language. 

The compiler starts to execute from the main method.