Java “Hello World” Program – Updated for Java 21

In any programming language, a ‘Hello World‘ program is considered a simple program that outputs Hello, World! on the screen. It is often used to verify that the runtime environment is set up correctly and that we are ready to develop real-world applications.

In this Java tutorial, we will learn to write our first “Hello World” program in Java.

1. Java Hello World Program – Java 21 and Later

Since Java 21, we can use unnamed classes and instance main methods that allow us to bootstrap a class with minimal syntax. It is aimed to benefit mostly beginners who have just started to learn Java and want to try out the language syntax for quick learning.

The following code is a fully functional class that will print the output “Hello, World!” to the console. We can store this class in any Java file such as HelloWorld.Java and then we can run it.

void main() { 

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

When we run the application, it prints the message in the console.

Hello, World!

2. Java Hello World Program [ ≤Java 20]

The following program is the simplest and most verbose Java program that prints the “Hello, World!” in the output console or prompt. It defines a class, HelloWorld.

Note that the class name can be anything. We must ensure that the class is stored in a file with the same name.

public class HelloWorld {

    public static void main(String[] args) {

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

We can copy the above program and paste it directly into the editor. Still, I will recommend you write it yourself. This will help understand the difference in Syntax mainly the lower or upper case in keywords, classes, and methods.

Do not forget to save the Java file with name In Java, a class name and the file (in which it is written) must be the same.

3. Compiling and Executing the Program

Java does not run the source files directly. We must compile the source .java file to .class file that JVM can understand and execute. Note that Java is case sensitive so make sure that the capitalization of the filename matches the class name. 

$ javac

The javac command will create the HelloWorld.class file in the same directory that contains the bytecode version of the program. Remember, bytecode is not executable code. Bytecode must be executed by a Java Virtual Machine.

Now we can execute the class file using the java command that uses the Java interpreter to run the program.

$ java HelloWorld.class

Notice the program has run and the output is displayed in the terminal. The program completes and ends immediately after printing the message as there is nothing to do in the program after this.

Hello, World!

Before moving on, it is important to mention that beginning with JDK 11, Java provides a way to run some types of simple programs directly from a source file, without explicitly invoking javac. But the limitation is that the program cannot use any external dependencies other than the java.base module. And the program can be only a single-file program.

$ java   //Prints Hello World!

4. How does It Work?

Java is object oriented programming language. Everything in Java is encapsulated inside a Java class. In this case, the class name is HelloWorld.

The HelloWorld class contains the main() method which is the single starting point for JVM to start the execution of any program written in Java. Remember that we must provide the exact syntax of the main() method in the program which we want to execute.

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

Let us note down a few important observations from this program:

  • public means that all other classes can access it.
  • static means that we can run this method without creating an instance of HelloWorld.
  • void means that this method doesn’t return any value.
  • main is the name of the method.
  • String[] is the type that is used to refer to text content in Java. The [ ] brackets indicate that it is of array type.
  • args is the name of the method argument, which is of type String[]. It means that the main method can accept multiple text inputs while starting the program execution. These arguments are generally user inputs to the program.
  • System.out.println is the instruction given to JVM to print the given string Hello, World! to the console (default output target).

Happy Learning !!

Java Hello World Sourcecode


Notify of
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.