Java Hello World Program Explained Line by Line
Introduction
The Hello World program is the traditional first program in any language. In Java it is only a handful of lines long, yet almost every word in it has a meaning worth understanding. This tutorial explains the Java Hello World program line by line. We will start by writing, compiling and running the program. We will then go through every keyword and symbol in it. Next we will pass input to the program through command-line arguments, and look at the most common beginner mistakes and the errors they cause. Lastly, we will see the shorter form of Hello World which newer Java versions allow.
(Note - You need a JDK installed to follow along. See How to Install the JDKClick to read tutorial: How to Install the JDK and Set JAVA_HOME and PATH if you do not have one yet.) Writing and running Hello World Create a file named
Open a terminal in the folder containing the file, compile it with
OUTPUT of the above code
Compiling created a file named
Line 1 - the comment
Passing command-line arguments
Let us now use
OUTPUT of the above code
Explanation of the code
1. Forgetting a semicolon
OUTPUT on compiling the above code
The error points to line 4, and the caret
2. A public class in a file with a different name - here the class
OUTPUT on compiling the above code
Other common mistakes are typing
(Note - You need a JDK installed to follow along. See How to Install the JDKClick to read tutorial: How to Install the JDK and Set JAVA_HOME and PATH if you do not have one yet.) Writing and running Hello World Create a file named
HelloWorld.java - the name and capitalization must be exactly the same as the class name - and type in this code.
The Java Hello World program
//HelloWorld.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
javac, and run it with java. Note that java takes the class name, without .class at the end.
javac HelloWorld.java java HelloWorld
Hello, World!
HelloWorld.class, which contains the program's bytecode. Running started a Java Virtual Machine, which loaded that class and ran it. Let us now understand each line.
//HelloWorld.java is a comment. Everything after // on a line is ignored by the compiler. Comments are notes for people reading the code. (We put the file name in a comment at the top of every example on JavaBrahman.)
Line 2 - public class HelloWorld {
classdeclares a class. In Java, all code lives inside classes - you cannot write a method on its own outside a class.HelloWorldis the name of the class. By convention, class names start with a capital letter, and each new word is also capitalized.publicis an access modifier which makes the class visible to all other code. A public class must be saved in a file with exactly the same name -HelloWorldmust be inHelloWorld.java.- The opening curly brace
{starts the body of the class, and the matching}on the last line ends it.
public static void main(String[] args) {
This line declares the main method - the entry point where the JVM starts running the program. Each word has a purpose -public- the JVM is outside the class, somain()must be public for the JVM to call it.static- the method belongs to the class itself, not to an object of the class. So the JVM can callmain()without first creating aHelloWorldobject. See static vs instance methodsClick to read tutorial: Static vs Instance Methods in Java for more.void- the return type.voidmeans the method does not return any value.main- the name of the method. The JVM looks for a method with exactly this name.String[] args- the method's parameter. It is an array of strings, which holds any command-line arguments passed to the program.argsis just a name; you could call it something else, butargsis the convention.{starts the body of the method.
System.out.println("Hello, World!");
This is a statement - an instruction for the computer to carry out.Systemis a class from Java's standard library (in thejava.langpackage, which every program can use without importing it).outis a field ofSystemwhich represents the standard output - normally the console.println()is a method which prints its argument followed by a new line. Its partnerprint()prints without a new line."Hello, World!"is a string literal - text inside double quotes.- The semicolon
;ends the statement. Every statement in Java ends with one.
} closes the body of main(), and the second closes the body of the class. Every opening brace must have a matching closing brace. Indenting the code inside each pair of braces, as in the example, makes it easy to see which brace closes which block.
args. Any words you type after the class name when running the program are passed to main() in this array.
Hello World with command-line arguments
//HelloName.java
public class HelloName {
public static void main(String[] args) {
System.out.println("Number of arguments: " + args.length);
if (args.length > 0) {
System.out.println("Hello, " + args[0] + "!");
} else {
System.out.println("Hello, World!");
}
}
}
java HelloName Tom
Number of arguments: 1 Hello, Tom!
args.lengthis the number of arguments passed - here, 1.args[0]is the first argument,"Tom". Array positions start at 0.- The
+operator joins strings together. When the program is run without arguments, theelsebranch printsHello, World!instead.
1. Forgetting a semicolon
A missing semicolon
//Greeting.java
public class Greeting {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}
Greeting.java:4: error: ';' expected
System.out.println("Hello, World!")
^
1 error
^ shows where the compiler expected the ;.2. A public class in a file with a different name - here the class
HelloWorld has been saved in a file named Hello.java.
A public class saved in the wrong file
//Hello.java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Hello.java:2: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
^
1 error
system or String with the wrong capitalization - Java is case-sensitive, so system and System are different names - and running java HelloWorld.class instead of java HelloWorld.
A shorter Hello World in Java 25
Java 25 finalized compact source files and instance main methods, which let small programs leave out the class declaration and the public static and String[] args parts. On Java 25 and later, this is a complete program -
Hello World on Java 25 and later
void main() {
IO.println("Hello, World!");
}
IO.println() is a newer, simpler way of printing to the console. This short form is handy for small programs and scripts. Throughout this series, however, we use the full form with a class and public static void main(String[] args) - it works on every Java version, and it is what you will see in almost all existing Java code.
Summary
In this tutorial we first wrote, compiled and ran the Java Hello World program. We then went through it line by line - the comment, the class declaration, the main() method with its public, static, void and String[] args, the System.out.println() statement, and the closing braces. Next we passed command-line arguments to a program through args. We then looked at two common beginner mistakes and the compiler errors they cause. Lastly, we saw the shorter Hello World which Java 25 allows. In the next tutorial, we will look more closely at compiling and running Java programsClick to read tutorial: Compiling and Running Java Programs with javac and java.Java Fundamentals Tutorials
Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code Compiling and Running Java Programs with javac and javaClick to read tutorial: Compiling and Running Java Programs with javac and java What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples How to Install the JDK and Set JAVA_HOME and PATHClick to read tutorial: How to Install the JDK and Set JAVA_HOME and PATH How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT JShell Tutorial: Java's Interactive REPL with ExamplesClick to read tutorial: JShell Tutorial: Java's Interactive REPL with Examples
Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS CodeClick to read tutorial: Choosing a Java IDE: IntelliJ IDEA vs Eclipse vs VS Code Compiling and Running Java Programs with javac and javaClick to read tutorial: Compiling and Running Java Programs with javac and java What is Java? History, Features, Editions and UsesClick to read tutorial: What is Java? History, Features, Editions and Uses JDK vs JRE vs JVM - Differences Explained with ExamplesClick to read tutorial: JDK vs JRE vs JVM - Differences Explained with Examples How to Install the JDK and Set JAVA_HOME and PATHClick to read tutorial: How to Install the JDK and Set JAVA_HOME and PATH How Java Code Runs: Bytecode, Class Loader and JITClick to read tutorial: How Java Code Runs: Bytecode, Class Loader and JIT JShell Tutorial: Java's Interactive REPL with ExamplesClick to read tutorial: JShell Tutorial: Java's Interactive REPL with Examples