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.
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:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}Compile and run it:
javac HelloWorld.java
java HelloWorldHello, 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:
Line 3: System.out.println("Hello, World!"); prints the text and then moves to a new line.
System.outis the standard output stream, normally your terminal.printlnmeans "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 }.
print, println and printf#
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));
}
}Java is fun.
New line.
Asha is 21 years old.
2 + 3 = 5printdoes not add a newline;printlndoes.printfformats values into placeholders:%sfor text,%dfor whole numbers,%.2ffor a decimal with 2 places, and%nfor a newline.+joins strings. The parentheses in(2 + 3)make Java add the numbers first; without them,"2 + 3 = " + 2 + 3would give2 + 3 = 23.
Escape sequences#
A backslash inside a string starts an escape sequence:
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");
}
}Name: Asha
She said "hi".
Path: C:\Users\ashaReading compiler errors#
If something is wrong, javac stops and reports where. For example, without the semicolon:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!")
}
}javac reports:
HelloWorld.java:3: error: ';' expected
System.out.println("Hello, World!")
^
1 errorThe 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:
Name: Asha
City: Pune
Score: 92.50Show hint
Use \t for the tab. In printf, %.2f prints a number with two decimal places, and %n ends the line.
Show solution
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);
}
}Name: Asha
City: Pune
Score: 92.50Key takeaway#
Finished this lesson?