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:
public class HelloWorld— This creates a container (class) namedHelloWorldthat holds your codepublic static void main(String[] args)— The entry point you learned about in the previous lessonSystem.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.