Java Hello World Program

Naveen AnamaneniUpdated · 5 min read

Every programmer's first program prints a short greeting. It sounds too simple to matter, but it proves that your whole setup works: the editor, the compiler and the JVM. If "Hello, World!" appears on your screen, everything after this is just learning the language.

Write the Program

Create a folder for your Java practice, for example java-practice in your Documents folder. Open any plain text editor (Notepad, TextEdit in plain-text mode, or VS Code) and type this program exactly as shown. Don't copy it with the formatting of a web page or word processor.

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

Save it in your folder as HelloWorld.java. The file name must match the class name on line 1 exactly, including the capital H and W.

Compile and Run It

Open a terminal in your practice folder. Then run two commands: javac compiles, java runs.

Terminal
cd %USERPROFILE%\Documents\java-practice
javac HelloWorld.java
java HelloWorld
  1. javac HelloWorld.java reads your file, checks it, and writes HelloWorld.class next to it. If it prints nothing, that's good news: no errors.
  2. java HelloWorld starts the JVM, loads HelloWorld.class and runs it. Notice there's no .class at the end of this command. You give it the class name, not the file name.

The terminal prints Hello, World!. You've just written, compiled and run a Java program.

What Each Line Means

Each line of Hello World, and how it becomes output

You don't need to understand every word on day one. Here is the short version; later lessons go deeper.

LineCodeMeaning for now
1public class HelloWorld {Creates a class named HelloWorld. In Java, all code lives inside a class.
2public static void main(String[] args) {The main method: the point where your program starts. The lesson "The Main Method in Java" explains every word.
3System.out.println("Hello, World!");Prints the text in quotes, then moves to a new line.
4}Ends the main method.
5}Ends the class.

Two small rules you'll use in every program:

  • Every statement ends with a semicolon ;.
  • Curly braces { } come in pairs. Every { needs a matching }.

println prints and then moves to the next line. print prints and stays on the same line. Here's a small ticket printed with both.

TicketPrint.java▶ Try it
public class TicketPrint {
    public static void main(String[] args) {
        System.out.println("Chennai Express");
        System.out.println("Coach S4, Seat 32");
        System.out.print("Fare: ");
        System.out.print("Rs. 450");
        System.out.println();
        System.out.println("Have a safe journey!");
    }
}
Output
Chennai Express
Coach S4, Seat 32
Fare: Rs. 450
Have a safe journey!

How it works

  1. Lines 3 and 4 use println, so each one ends with a new line.
  2. Lines 5 and 6 use print, so Fare: and Rs. 450 end up on the same line.
  3. Line 7, an empty println(), just finishes that line.
  4. Line 8 prints the last line.

Try it yourself: change line 5 from print to println. Before you run it, predict where Rs. 450 will appear.

Common Mistakes

Saving the file with the wrong name. A public class HelloWorld must be in HelloWorld.java. Saving it as helloworld.java or Hello.java gives:

Compiler output
Hello.java:1: error: class HelloWorld is public, should be declared in a file named HelloWorld.java
public class HelloWorld {
       ^
1 error

Adding .class when you run it. java HelloWorld.class doesn't work; Java looks for a class literally named HelloWorld.class:

Terminal output
Error: Could not find or load main class HelloWorld.class
Caused by: java.lang.ClassNotFoundException: HelloWorld.class

Forgetting the semicolon or writing system with a small s. Java is case-sensitive: System and system are different words. The lesson "Common Beginner Compile Errors" shows how to read and fix these messages.

Practice

  1. Easy: Change the program to print your name and your city on two separate lines.
  2. Harder: Print this box using only println, with the edges lined up exactly:
Output
+------------+
| Hello Java |
+------------+
Show a hint

Use three println lines, one for each line of the box. Count the characters: each line is 14 characters wide.

Quick check: Hello World

4 questions
1Which command compiles HelloWorld.java?
2Which command runs the compiled program?
3What is the difference between print and println?
4Your class is public class Bill. What must the file be called?

Key Takeaways

Was this tutorial helpful?

Lesson 12 of 482