Sub topic 3
Your First Program: Hello, World!
In the last chapter, we looked at how Java programs are structured: every program lives inside a class, and execution starts at a special method called main. Now it's time to put that into practice. You will write, compile, and run your first Java program.
What You Need
To follow along, you need two things installed:
- A JDK (Java Development Kit): a free download that includes the compiler and the tools to run Java programs. You can get it from vendors like Eclipse Temurin or Oracle.
- A text editor: anything that saves plain text files, such as Notepad, TextEdit, or VS Code.
You will also use a terminal (also called a command line): a window where you type commands instead of clicking buttons.
The Code
Create a file named HelloWorld.java. The file name must match the class name exactly, including capitalization. Type this in:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Let's read it line by line:
public class HelloWorldstarts a class named HelloWorld. A class is a container for code. The wordpublicmeans other code can see it.public static void main(String[] args)declares themainmethod. Java always starts running your program here.System.out.println("Hello, World!");prints text to the screen and moves to a new line. The text between the quotes is a String, which is Java's type for text. The semicolon ends the statement.- The two closing braces
}close the method and the class. Every opening brace{needs a matching closing brace.
Compile and Run
Java runs in two steps. First, you compile the code: the compiler translates your human-readable source into bytecode, a compact set of instructions stored in a .class file. Then the JVM (Java Virtual Machine) runs that bytecode on your computer.
Open a terminal in the folder where you saved the file and type:
javac HelloWorld.java
java HelloWorldjavac is the compiler. If it succeeds, you will see a new file, HelloWorld.class. The java command launches the JVM and runs your program. You should see:
Hello, World!callout tip If javac is not recognized, the JDK is not installed or not on your system's PATH (the list of folders your terminal searches for programs). Reinstall the JDK and restart your terminal. callout
The Two-Step Picture
Here is the flow from your source file to output on the screen:
Why the Two Steps Matter
This is the same bytecode idea from the first chapter. Your .class file does not contain instructions for one specific processor. It contains JVM instructions. That means you can compile once on Windows and run the same file on a Mac or Linux machine, as long as it has a JVM. This is why Java is called platform independent: "write once, run anywhere."
Common First-Program Errors
Almost everyone hits one of these early on:
- File name mismatch: the file must be
HelloWorld.javaif the class isHelloWorld. - Missing semicolon: Java statements end with
;. - Mismatched braces: count your
{and}. - Case sensitivity:
Systemwith a capital S is required;systemwill not compile.
Reading the compiler's error message carefully is a skill worth building now. It usually tells you the line number and what went wrong.
Exercise
What two commands do you run to compile and then execute HelloWorld.java?
Show answer
First javac HelloWorld.java to compile it into bytecode, then java HelloWorld to run it on the JVM.
Exercise
Why must the file name match the public class name?
Show answer
The compiler uses the class name to name the output file, and Java's tools expect HelloWorld.class to contain the class HelloWorld. A mismatch causes a compile error.