Skip to content
Laviyx Learn

Try

Java course menu

Your First Java Program: Hello World Explained Line by Line

Write, compile and run your first Java program. Understand the class, public static void main(String[] args), System.out.println vs print vs printf, escape sequences, and how to read compiler errors.

Beginner6 min readUpdated
On this page

The traditional first program prints a greeting. In Java it takes a few more lines than in some languages, and each of them teaches you something about how Java programs are built.

The program#

Save this as HelloWorld.java:

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

Compile and run it:

Terminal
javac HelloWorld.java
java HelloWorld
Output
Hello, World!

Line by line#

Line 1: public class HelloWorld { declares a class named HelloWorld. All Java code lives inside classes. public means it can be used from anywhere, and a public class must be saved in a file with the same name (HelloWorld.java). The opening brace starts the class body.

Line 2: public static void main(String[] args) { declares the main method, where the program starts. Every word matters:

PartMeaning
publicThe JVM can call it from outside the class
staticIt belongs to the class, so it can run without creating an object first
voidIt returns no value
mainThe name the JVM looks for
String[] argsCommand-line arguments, as an array of strings

Line 3: System.out.println("Hello, World!"); prints the text and then moves to a new line.

  • System.out is the standard output stream, normally your terminal.
  • println means "print line".
  • "Hello, World!" is a string literal in double quotes.
  • The semicolon ends the statement.

Lines 4 and 5 close the method and the class. Every { needs a matching }.

Java
public class Main {
    public static void main(String[] args) {
        System.out.print("Java ");
        System.out.print("is ");
        System.out.println("fun.");
        System.out.println("New line.");
        System.out.printf("%s is %d years old.%n", "Asha", 21);
        System.out.println("2 + 3 = " + (2 + 3));
    }
}
Output
Java is fun.
New line.
Asha is 21 years old.
2 + 3 = 5
  • print does not add a newline; println does.
  • printf formats values into placeholders: %s for text, %d for whole numbers, %.2f for a decimal with 2 places, and %n for a newline.
  • + joins strings. The parentheses in (2 + 3) make Java add the numbers first; without them, "2 + 3 = " + 2 + 3 would give 2 + 3 = 23.

Escape sequences#

A backslash inside a string starts an escape sequence:

SequenceMeaning
\nNew line
\tTab
\"A double quote
\\A backslash
Java
public class Main {
    public static void main(String[] args) {
        System.out.println("Name:\tAsha");
        System.out.println("She said \"hi\".");
        System.out.println("Path: C:\\Users\\asha");
    }
}
Output
Name:	Asha
She said "hi".
Path: C:\Users\asha

Reading compiler errors#

If something is wrong, javac stops and reports where. For example, without the semicolon:

Java
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello, World!")
    }
}

javac reports:

Text
HelloWorld.java:3: error: ';' expected
        System.out.println("Hello, World!")
                                           ^
1 error

The message gives the file, the line number, what was expected, and a caret pointing at the spot. Fix the first error first; later ones are often caused by it.

Common mistakes#

Practice#

Practice exercise

Write a program that prints this profile, using printf for the last line:

Text
Name:	Asha
City:	Pune
Score:	92.50
Show hint

Use \t for the tab. In printf, %.2f prints a number with two decimal places, and %n ends the line.

Show solution
Java
public class Profile {
    public static void main(String[] args) {
        System.out.println("Name:\tAsha");
        System.out.println("City:\tPune");
        System.out.printf("Score:\t%.2f%n", 92.5);
    }
}
Output
Name:	Asha
City:	Pune
Score:	92.50

Key takeaway#

Finished this lesson?