Printing Output in Java
A shop receipt printer moves its paper one line at a time. Sometimes it prints a whole line, sometimes just a word and then waits for the price to follow on the same line. Java's console output works the same way. You've used System.out.println since Hello World; this lesson adds its two siblings, print and printf, and shows exactly where the next piece of text lands after each one.
Three Ways to Print
| Method | Prints | Then |
|---|---|---|
System.out.println(x) | x | Moves to a new line |
System.out.print(x) | x | Stays on the same line |
System.out.printf(format, values...) | The format, with values filled in | Stays, unless the format contains %n |
public class Receipt {
public static void main(String[] args) {
System.out.println("Fresh Mart");
System.out.println("----------");
System.out.print("Rice: ");
System.out.print("Rs. ");
System.out.println(340);
System.out.println();
System.out.printf("Total: Rs. %d for %d items%n", 520, 3);
}
}Fresh Mart
----------
Rice: Rs. 340
Total: Rs. 520 for 3 itemsHow it works
- Lines 3 and 4 use
println, so each one ends its line. - Lines 5 and 6 use
print, soRice:andRs.stay on the same line. Line 7 adds340and finishes the line. - Line 8, an empty
println(), prints a blank line. - Line 9 uses
printf: each%dis replaced by the next value, in order, and%nends the line. The next lesson, "Format Specifiers", covers every%code.
Try it yourself: remove %n from line 9 and add another println("Thank you") after it. Where does "Thank you" appear?
Escape Sequences
Some characters can't be typed directly inside a string. Write them with a backslash:
| Escape | Means | Example | Prints |
|---|---|---|---|
\n | New line | "Line 1\nLine 2" | two lines |
\t | Tab | "Item\tPrice" | Item and Price with a tab between |
\" | Double quote | "He said \"hi\"" | He said "hi" |
\\ | Backslash | "C:\\data" | C:\data |
\' | Single quote (in a char) | '\'' | ' |
public class Escapes {
public static void main(String[] args) {
System.out.println("Item\tQty\tPrice");
System.out.println("Dal\t2\tRs. 180");
System.out.println("Path: C:\\bills\\nov.txt");
System.out.println("Shop says \"Thank you!\"");
}
}Item Qty Price
Dal 2 Rs. 180
Path: C:\bills\nov.txt
Shop says "Thank you!"Tabs line up columns only roughly, because a tab jumps to the next tab stop. For columns that line up exactly, use printf with widths, as the next lesson shows.
Printing Several Values
println takes one value. To print several, join them with +, or use printf:
Joined with + | With printf |
|---|---|
println(name + " paid Rs. " + amount) | printf("%s paid Rs. %d%n", name, amount) |
Both work. + is fine for short messages; printf is clearer when there are many values or when you need neat columns and decimals.
Common Mistakes
Forgetting brackets when adding numbers. println("Total: " + 2 + 3) prints Total: 23. Write "Total: " + (2 + 3).
Using print and wondering why the prompt looks stuck. After print, the next output, or the user's typing, continues on the same line. That's exactly what you want for prompts like Enter age: .
Mismatched printf values. printf("%d %d%n", 5) has two %d but one value, and crashes at runtime with MissingFormatArgumentException.
Practice
- Easy: Print your name, city and age on one line using three
printcalls and oneprintln. - Harder: Print a three-row table of items and prices using
\t, then again usingprintf. Which lines up better?
Key Takeaways
Was this tutorial helpful?