Java Hello World Program

We’ll cover the following

  • Introduction
  • Hello, World! word by word
  • Run


This lesson shows the how to use Java code to display the text “Hello, World!” on the console.

We will take a closer look at each element of this program:

public class Main         //line-1
{                         //line-2
   public static void main(String[] args) //line-3
   {   //line-4
       System.out.println("Hello, World!");
} //line-7

Hello, World! word by word

line-1 mark the declaration of a public class named Main:

public is a keyword of the Java language. It indicates that the element that follows should be made available to other Java elements.

In this case, what follows is a class named Main. As a result, this keyword indicates that the Main class is a public class, which means other classes can use it.

i)Java is case-sensitive: "MyClass" and "myclass" has different meaning.
ii)The name of the java file must match the class name.

class is another Java keyword. It indicates that the element being defined is a class.

All Java programs are made up of one or more classes. A class definition is a blue print for the behavior of the objects.

Main is a name that identifies the class being defined here.

The keywords, such as public and class, are words that are defined by the Java programming language.

The names are words that you create to identify various elements in your program.

In this case, the name Main identifies the public class being defined here.

Sometimes names are called identifiers.

A name is a type of identifier, but not all identifiers are names.

{, the opening brace on line-2 marks the beginning of the class body.

The end of the body is marked by the closing brace } on line 7.

Everything within these braces belongs to the class.

public static void main(String[] args) //line-3
   {   //line-4
       System.out.println("Hello, World!");
} //line-7

Lines 3 through 7 define a method of the Main class named main:

public in line-3 is the public keyword is used again. It indicates that a method should have public access. That means classes other than the Main class can use it.

All Java programs must have a class that declares a public method named main. The main method contains the statements that are executed when you run the program. It is the starting point of your program.

static in line-3 is required to declare the main method as the entrance point.

void in line-3 marks the return type of a method.

In Java, a method is a unit of code that can calculate and return a value.

If a method doesn’t need to return a value, you must use the void keyword to indicate that no value is returned.

Java requires that the main method should not return a value, you must specify void when you declare the main method.

main is the name to identify this method.

public static void main(String[] args) //line-3

(String[] args) is called a parameter list, and it’s used to pass data to a method.

Java requires that the main method must receive a single parameter that’s an array of String objects.

By convention, this parameter is named args.

{ in line-4 marks the main method body which ends at line 6.

These braces mark the body of the main method.

Whenever you come to a closing brace, it’s paired with the most recent opening brace that hasn’t already been closed.

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

System.out.println(“Hello, World!”); is the only statement in the entire program.

It calls a method named println that belongs to the System.out object.

The println method displays a line of text on the console.

Java statements ends with a semicolon.


To run this program, save it to in a text file named

Then you can compile it by running the following command at a command prompt:


This command creates a class file named Main.class that contains the Java byte code compiled for the Main class.

You can run the program by entering this command:

java Main

Because the file contains just one class, you can run it directly using the java command without first compiling.

If you type java Main and there is a .java file but no corresponding .class file, the java command will first compile the file and then run the resulting class.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java JDK Installation

Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy

One comment

Leave a Reply

Your email address will not be published.