Hello World Java Code Example: Simple Program Tutorial

Robert Jul 13, 2026

When it comes to learning the Java programming language, one of the first codes that every aspiring developer writes is the classic "Hello, World!" program. This simple yet powerful example serves as a great starting point, helping you understand the basic syntax and structure of Java. Let's dive in and explore this iconic code together.

Java Hello World Program For Beginners
Java Hello World Program For Beginners

The "Hello, World!" program in Java is a single line of code that simply displays the text "Hello, World!" on the console. However, before we write this code, we need to understand its breakdown and the essential components of a Java program.

C++ Hello world program explanation - Code for Java c
C++ Hello world program explanation - Code for Java c

Java Program Structure

Every Java program must follow a specific structure, known as the Java class structure. The "Hello, World!" program is no exception. It consists of the following fundamental elements:

C Hello World Program - BTech Geeks
C Hello World Program - BTech Geeks

1. Package declaration - Although optional, it's a good practice to include a package declaration at the beginning of every Java program. Package statements ensure that your code is organized and can be easily识别 and reused.

2. Import statements - To use predefined classes and interfaces from the Java API, you need to import them using the import statement.

Hello, World! Iconic Programming Phrase Art
Hello, World! Iconic Programming Phrase Art

Java Class

A Java program is written inside a class, which is a blueprint for creating objects. In the "Hello, World!" program, we'll create a class named Main, as it's the entry point of our application.

The Main Method

a computer screen with the text file editor highlighted
a computer screen with the text file editor highlighted

The Main method is the starting point of every Java application. It's a special method with a predefined signature, i.e., it must have a specific name, return type, and parameters. In Java SE 8 and later, you can use the functional interface java.lang Runnable to define the main method with a single parameter, which is an array of strings.

Building the "Hello, World!" Program

Now that we understand the basic structure of a Java program, let's create our "Hello, World!" example. We'll create a Java class named HelloWorld with a Main method that displays the "Hello, World!" message on the console.

some type of text that is in the language hello world with different types of words
some type of text that is in the language hello world with different types of words

The complete "Hello, World!" Java program looks like this:

package hello;

import java.util.Scanner;

public class HelloWorld {

    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

C# Hello World Program Example - Tutlane
C# Hello World Program Example - Tutlane
We Should Get Rid of "Hello, World!" Forever💩
We Should Get Rid of "Hello, World!" Forever💩
the word hello - world written in neon green and pink on a black background with arrows
the word hello - world written in neon green and pink on a black background with arrows
an info sheet with the words, how do you write hello world? and some type of text
an info sheet with the words, how do you write hello world? and some type of text
the words hello world are written in green and red letters on a black background,
the words hello world are written in green and red letters on a black background,
Here’s How Hello World in 15 Programming/Scripting Languages Looks Like
Here’s How Hello World in 15 Programming/Scripting Languages Looks Like
Hello World Program in Java - Scientech Easy
Hello World Program in Java - Scientech Easy
Coding
Coding
the word hello, world written in white on a black background
the word hello, world written in white on a black background
RESTEasy Hello World Example - Java Code Geeks
RESTEasy Hello World Example - Java Code Geeks
Printing "Hello World"
Printing "Hello World"
an image of a computer screen with the words print hello world
an image of a computer screen with the words print hello world
Hello World in X86 NASM Assembly
Hello World in X86 NASM Assembly
Hello, World! - Why Python is the Most Beginner-Friendly Coding Language
Hello, World! - Why Python is the Most Beginner-Friendly Coding Language
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
Structure of a Java Program + Hello World Example | Java Programming Basics 💻
First Program in Java(Hello, World!) | Basic Parameters use to execute a java code |
First Program in Java(Hello, World!) | Basic Parameters use to execute a java code |
Spring HelloWorld Example in Java Annotations and Autowiring [Tutorial]
Spring HelloWorld Example in Java Annotations and Autowiring [Tutorial]
Hello World In Programming
Hello World In Programming
Hello world in 5 different programming languages
Hello world in 5 different programming languages
Pirple.com: Sharpen your skills.
Pirple.com: Sharpen your skills.

Package and Import Statements

The first line of the code (package hello;) declares that the code belongs to a package named hello. The import java.util.Scanner; statement allows us to use the Scanner class from the Java Utility library for basic input/output operations. Although not necessary for this simple program, it's included to demonstrate the import process.

HelloWorld Class and Main Method

The HelloWorld class contains a single public static method named Main. This method is the entry point of the Java application. Inside the main method, we use the System.out object's println method to print the "Hello, World!" message to the console.

When you run the HelloWorld class, you'll see the following output in the console:

Hello, World!

Congratulations! You've just written your first Java program. As you continue your journey into the world of Java, you'll build upon this foundation to create more complex and exciting applications.

Start exploring other Java syntax elements, such as variables, data types, control structures, and object-oriented concepts like inheritance, polymorphism, and encapsulation. Online platforms like Oracle's Java Documentation, Oracle Java Tutorials, and coding communities like Stack Overflow can provide you with invaluable guidance and resources along the way.

Happy coding, and may your voyage into the Java world be filled with excitement, discovery, and endless learning!