Printing Output in Java

Naveen AnamaneniUpdated · 5 min read

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

Where the cursor ends up after print, println and printf
MethodPrintsThen
System.out.println(x)xMoves to a new line
System.out.print(x)xStays on the same line
System.out.printf(format, values...)The format, with values filled inStays, unless the format contains %n
Receipt.java▶ Try it
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);
    }
}
Output
Fresh Mart
----------
Rice: Rs. 340
 
Total: Rs. 520 for 3 items

How it works

  1. Lines 3 and 4 use println, so each one ends its line.
  2. Lines 5 and 6 use print, so Rice: and Rs. stay on the same line. Line 7 adds 340 and finishes the line.
  3. Line 8, an empty println(), prints a blank line.
  4. Line 9 uses printf: each %d is replaced by the next value, in order, and %n ends 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:

EscapeMeansExamplePrints
\nNew line"Line 1\nLine 2"two lines
\tTab"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)'\'''
Escapes.java▶ Try it
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!\"");
    }
}
Output
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

  1. Easy: Print your name, city and age on one line using three print calls and one println.
  2. Harder: Print a three-row table of items and prices using \t, then again using printf. Which lines up better?

Quick check: Printing output

4 questions
1Which method moves to a new line after printing?
2What does System.out.print("A"); System.out.print("B"); print?
3How do you print a double quote inside a string?
4Why prefer %n to \n in printf?

Key Takeaways

Was this tutorial helpful?

Lesson 47 of 482