Java Hello World Program


In this post, we will see how to write your first java program. This post is intended only for java beginners to acquaint them with the steps to write a simple java program. Simply put, we will write a code that outputs "Hello World!" to your console/screen.

For Folks having some knowledge in other programming languages, this is a basic program to print a statement on your console as you did earlier.

Prerequisite for running “Java Hello World Program”

This Java hello world program will be a very simple program that will print Hello, World! to console.

Before running the program, you need to make sure java is properly installed on your machine.

  • Install the JDK if you don’t have it installed, download the JDK and install it.
  • Set path of the JDK/bin directory. you can follow this link for setting the path

You can either write a program in IDE such as Eclipse or you can simply write it in text editor/Notepad save your code as a .java file. You can then compile java source code and execute your program in Command-Line. A detailed explanation is given below for this program.

You need to save this file as

How does “Java program to print Hello World” works?

Let’s see a detailed explanation of the Hello world program.

Class declaration

This is a comment in java and this statement will be ignored at run time.
As java is object-oriented programming, every java application should have a class definition.

  • Class declaration: A class declaration includes the name and visibility of the class.class HelloWorld is a declaration of class that includes the keyword, followed by an identifier HelloWorld
  • The class declaration is followed by curly braces {}, which define the class’s definition.

Main method

This is called the main method in java. This is the entry point for this program.

public: This is an access modifier that is used to define visibility. Here the main method will be visible to all other classes.
static: static members belong to the class rather than any specific object. It simply means you can access the static members without creating any objects.
void: void is another keyword that defines the return type of the main method. It simply means the main method does not return anything before terminating the program.
main: main is the method name here.
String args[]: You can pass arguments to the java program using the args[] array. It is used to take user input using the command line.

Print statement

System.out.println is used to print literals in double-quotes("") to console. As we have passed “Hello, World!” here, it will print Hello, World! to console.


As you can see, each statement is terminated with a semicolon(;). You can put new lines or spaces in the code but the statement has to be ended by a semicolon.

Compile and run the program

If you run this program in eclipse ide, you can simply right click and on run as java application.
You can compile this java program using the command line as below:
Open the command prompt and go to the location where you have saved

$ javac

You can run the program using the command line as below:

$ java HelloWorld

When you run the above program, you will get the below output:

When we execute a java program, we need to give the full class name without .java extension.

In how many ways can you write a Java Program?

We can formulate different ways to write a Java program by doing some modifications to the main method’s signature. Below some examples are shown on how we can achieve this.

1. By Changing the Subscript Notation of Input Array in Main Method

We can change the position of subscript notation of the input array passed in the main method for command-line input. We mean the args[] array’s subscript notation can be used after Data type String, before or after the variable name.

To be more precise the above code for printing “Hello World!” can also be written in the following ways:

You see we have written the args array subscript notation before and after the variable name. The output for each case will be the same, we just change the signature of the main method.

2. By Changing the Sequence of Modifiers in the method, without modifying Method Prototype

We can also write the program in alternate ways by changing the sequence of modifiers and keywords. Consequently changing the method declaration.

In simple terms, we can interchange the position of keywords public and static, still the program would work fine. As the order of keywords does not matter. Likewise, we can write the above program as :

Note: The position of the return type keyword should not be mingled with. The return type of the method should always come after the modifiers and other keywords. Hence this an Invalid method declaration in java:

public void static main(String args[])

3. By Replacing Array Subscript Notation with 3 ellipses (Dots) for var-args support

We can also replace the Array Subscript notation in the args array and provide 3 dots instead for taking command line argument support. So instead of writing the main method like this: public static void main(String[] args)

We can alternatively write the whole program as:

Resolving Error: “javac is not recognized as an internal or external command”?

Suppose, while compiling the program shown in the above examples if you get the following error given below:

So, if you encounter this error, it means that the Command Line or DOS does not recognize the command javac. To resolve this issue we need to set the path of Java Resources to the bin Directory of your Java Development Kit. It is a good practice to set the path but it is not required if you save all your java source files inside the bin Folder.

Resolving Error: “reached end of file while parsing”

Again, while compiling any program due to some shallow mistakes, you might encounter the below given error:

This is a common error in Java and you need not worry about this. To resolve this error you just need to check whether all the Curly Braces i.e. { and } are opened and closed properly.

It is a good practice to check the braces while coding otherwise, for huge lines of code finding the missing braces can be a worrisome task.


You need to print Yeah!! I executed my first java program on console and name of the class should be MyFirstJavaProgram

Important points

Let’s go through some important points about Hello world program.

  • Any java source file can have multiple classes but it can have only one public class.
  • Java source file name should be the same as the public class name. That’s why I said above “you need to save this file as“.
  • When you compile the java file, it is converted to byte code with .class extension, so you should be able to see HelloWorld.class in the directory.
  • When you execute the program, JVM looks for the main method and will execute it. You need to be very careful with the signature of the main method otherwise, the program may throw java.lang.NoSuchMethodError: main.

Was this post helpful?

Related Posts

  • 29 November

    How to Get Variable From Another Class in Java

    Table of ContentsClasses and Objects in JavaAccess Modifiers in JavaGet Variable From Another Class in JavaUsing the Default or Public Access Modifier of the Other ClassUsing the Static Member of Another ClassUsing the Inheritance Concept of JavaUsing the Getters and Setters of Another ClassUsing the Singleton Pattern Design for Declaring Global VariablesConclusion In this article, […]

  • 24 September

    Increment for Loop by 2 in Java

    Table of ContentsHow to increment for loop by 2 in JavaHow to increment for loop by n in Java In this post, we will see how to increment for loop by 2 in Java. There are several looping statements available in Java and one of them is for loop in java. There are three parts […]

  • 20 September

    Return ArrayList in Java

    Table of ContentsReturn ArrayList in Java From a Static MethodReturn ArrayList in Java From a Non-static MethodConclusion This article discusses cases of returning an ArrayList in Java from a method. An ArrayList in Java is a collection of elements of the same data type under a single variable name. In different cases, you can return […]

  • Check if Object is null Java
    14 March

    Check if Object Is Null in Java

    Table of ContentsComparison Operator to Check if Object Is Null in JavaisNull() Method to Check if Object Is Null in JavanonNull() Method to Check if Object Is Null in JavarequireNonNull() Method to Check if Object Is Null in JavaConclusion An object in Java is an instance of a class. It is a real entity existing […]

  • How to print multiple variables in java
    31 January

    How to Print Multiple Variables in Java

    Table of ContentsWays to Print Multiple Variables in JavaUsing System.out.printUsing System.out.printfPrint Multiple Variables in Java Using LoggerFrequently Asked QuestionsHow Do I Print Multiple Values on One Line in JavaHow to Print Multiple Integers in JavaHow to Print String and Integer in Same Line in Java In this post, we will see how to print multiple […]

  • What is == in java
    01 December

    What is == in java

    Table of ContentsWhat is meaning of == in JavaCode example of ==Object equality using equal to ==The difference between == and equals== in EnumConclusion In this tutorial, we will learn what is == in Java as it is a common lexeme that we will use while developing applications. The == lexeme can be confused with […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.