JB Header

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 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!");
  }
}
Open a terminal in the folder containing the file, compile it with javac, and run it with java. Note that java takes the class name, without .class at the end.
javac HelloWorld.java
java HelloWorld
 OUTPUT of the above code
Hello, World!
Compiling created a file named 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.
Line 1 - the comment //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 {
  • class declares a class. In Java, all code lives inside classes - you cannot write a method on its own outside a class.
  • HelloWorld is the name of the class. By convention, class names start with a capital letter, and each new word is also capitalized.
  • public is 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 - HelloWorld must be in HelloWorld.java.
  • The opening curly brace { starts the body of the class, and the matching } on the last line ends it.
Line 3 - 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, so main() 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 call main() without first creating a HelloWorld object. See static vs instance methodsClick to read tutorial: Static vs Instance Methods in Java for more.
  • void - the return type. void means 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. args is just a name; you could call it something else, but args is the convention.
  • { starts the body of the method.
Line 4 - System.out.println("Hello, World!"); This is a statement - an instruction for the computer to carry out.
  • System is a class from Java's standard library (in the java.lang package, which every program can use without importing it).
  • out is a field of System which represents the standard output - normally the console.
  • println() is a method which prints its argument followed by a new line. Its partner print() 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.
Lines 5 and 6 - the closing braces The first } 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.
Passing command-line arguments Let us now use 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
 OUTPUT of the above code
Number of arguments: 1
Hello, Tom!
Explanation of the code
  • args.length is 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, the else branch prints Hello, World! instead.
Common beginner mistakes The compiler reports mistakes with the file name, the line number and a description. Here are the two you are most likely to meet first.

1. Forgetting a semicolon
A missing semicolon
//Greeting.java
public class Greeting {
  public static void main(String[] args) {
    System.out.println("Hello, World!")
  }
}
 OUTPUT on compiling the above code
Greeting.java:4: error: ';' expected
    System.out.println("Hello, World!")
                                       ^
1 error
The error points to line 4, and the caret ^ 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!");
  }
}
 OUTPUT on compiling the above code
Hello.java:2: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
       ^
1 error
Other common mistakes are typing 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.