Course contentsShow
← Java
Lesson 11 of 2,8701. Java Setup and First ProgramFree lesson

Writing Your First Hello World Program

Creating a simple Java program that prints 'Hello, World!' using System.out.println().

Writing Your First Hello World Program

You'll learn: How to create a complete Java program that displays "Hello, World!" on your screen—your first working code!

The Core Idea

Now that you understand the main method is where Java programs start, it's time to write your first complete program. A "Hello, World!" program is the traditional first step in learning any programming language—it proves your setup works and shows you the basic structure of a working program.

Building Your Hello World Program

Think of writing a Java program like writing a letter: you need the right format and structure. Here's the complete code:

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

Let's break down what's happening:

  1. public class HelloWorld — This creates a container (class) named HelloWorld that holds your code
  2. public static void main(String[] args) — The entry point you learned about in the previous lesson
  3. System.out.println("Hello, World!"); — This is the instruction that prints text to your screen

The System.out.println() command is like asking Java to write a message on your computer screen. The text inside the quotes is exactly what will appear.

Trying It Out

Save this code in a file named HelloWorld.java (the filename must match the class name). Then use javac to compile it and java to run it—tools you learned about when exploring the JDK.

Key Takeaway: Your first Java program combines a class structure, the main method entry point, and System.out.println() to display output—proving your development environment works and giving you a foundation to build upon.