Java Output
Showing results is the first thing every program needs. Java prints to the console through the System.out object, which offers three main methods: println, print and printf.
println and print
println prints its argument and then a line break. print prints the argument and stays on the same line:
class Main {
public static void main(String[] args) {
System.out.print("Loading");
System.out.print(".");
System.out.print(".");
System.out.println(".");
System.out.println("Done!");
System.out.println(); // an empty line
System.out.println("Bye");
}
}Loading...
Done!
Bye
Calling println() with no argument prints just a line break.
Printing numbers and text
Text must be inside double quotes. Numbers are written without quotes, and Java will happily calculate before printing:
class Main {
public static void main(String[] args) {
System.out.println(42);
System.out.println(3 + 4);
System.out.println("3 + 4");
System.out.println("Sum: " + 3 + 4);
System.out.println("Sum: " + (3 + 4));
}
}42
7
3 + 4
Sum: 34
Sum: 7
Look closely at the last two lines. The + operator works left to right: "Sum: " + 3 produces the text "Sum: 3", and adding 4 to text simply appends it. Parentheses force the addition to happen first.
Tip: When you mix text and arithmetic in one expression, wrap the arithmetic in parentheses.
Escape sequences
Some characters can't be typed directly inside a string. Use a backslash to escape them:
| Escape | Result |
|---|---|
\n |
new line |
\t |
tab |
\" |
double quote |
\\ |
backslash |
class Main {
public static void main(String[] args) {
System.out.println("Line one\nLine two");
System.out.println("Name\tScore");
System.out.println("She said \"hello\"");
System.out.println("C:\\Users\\java");
}
}Line one
Line two
Name Score
She said "hello"
C:\Users\java
Formatted output with printf
printf takes a format string with placeholders, followed by the values to put in them. It does not add a line break automatically, so finish the format with %n (or \n).
| Placeholder | Used for | Example output |
|---|---|---|
%s |
any value as text | Ada |
%d |
whole numbers | 42 |
%f / %.2f |
decimal numbers (2 decimals) | 3.14 |
%5d / %-8s |
padded to a width (right / left aligned) | 42 |
%n |
platform line break |
class Main {
public static void main(String[] args) {
String item = "Coffee";
int qty = 3;
double price = 2.5;
System.out.printf("%s x%d = %.2f EUR%n", item, qty, qty * price);
System.out.printf("|%-8s|%5d|%n", "left", 42);
String line = String.format("Pi is about %.3f", Math.PI);
System.out.println(line);
}
}Coffee x3 = 7.50 EUR
|left | 42|
Pi is about 3.142
String.format uses the same placeholders but returns the text instead of printing it — handy when you need the formatted string later.
Test yourself
What is the difference between print and println?
printlnis fasterprintlnadds a line break after the textprintonly prints numbers- There is no difference
What does System.out.println("Total: " + 2 + 3); print?
- Total: 5
- Total: 23
- Total: 2 + 3
- A compile error
Which placeholder prints a double with exactly two decimals in printf?
- %d
- %2d
- %.2f
- %s2