Concepts / Text Input and Output Fundamentals

Text Input and Output Fundamentals

System.out.print(x) outputs x after converting it to a string when necessary.

  • Programming

When the Text Is Right but the Layout Is Wrong

Suppose a Java program prints the correct words, yet the console shows them on the wrong lines or jammed together. It is tempting to look only at the values being displayed. In many cases, though, the value is not the problem. The real issue is the position of the next piece of text. Every output operation does two jobs: it displays text, and it decides where later text will begin. This article separates those two jobs so you can trace them one at a time.

What do you think happens?

Before running it, predict what the console shows for these two statements: System.out.print("A"); followed by System.out.print("B");

  • A on one line and B on the next line

  • AB on a single line

  • B only, because the second statement replaces the first

Reveal answer
Answer:

AB on a single line

Both calls use print. Print displays its text and does not add a line feed, so the text from the second call continues directly after the first.

Tracing Two Print Calls

Start with the simplest case. The statement System.out.print(x) outputs x. If x is not already text, the value is converted to a string when necessary before it is output. Each print call sends its text to standard output, which is generally where a user sees program text in the programs covered here. After a print call finishes, the output position stays where the printed text ended, so the next output continues on the same line.

java
Output
AB
Notes on the code
  • The first call displays A and leaves the position just after the A.
  • The second call displays B beside the A, because print adds no line feed.
  • The console therefore shows the single line AB.
Console after print AA, cursor after AConsole after print BAB, cursor still on samelineprint B
After System.out.print("A") and then System.out.print("B"), where does B appear relative to A? Print keeps the position on the same line.

The second statement is the trace point that matters most. It both displays B and changes the position for future output. Because print leaves the position on the same line, anything printed later will also begin right after the B unless some other operation moves to a new line.

Print Versus Println

Definition

System.out.println(x) outputs the same text that System.out.print(x) would output, and then adds a line feed. A line feed moves the output position to the start of the next line.

The two methods share the same display behavior. The difference is what happens after the text is shown. Print ends by leaving the position where the text stopped. Println ends by moving the position down to a new line. Once you know this, you can predict the layout of any sequence of output calls by asking one question for each statement: does this operation end by moving to a new line?

java
Output
A
BC
Notes on the code
  • The println call displays A and then adds a line feed.
  • The print call displays B on the new line, and the position stays after B.
  • The next print call displays C directly after B, so B and C share a line.
StatementText displayedLine feed addedWhere later output begins
System.out.print(x)Yes, the value of x as textNoRight after the displayed text, on the same line
System.out.println(x)Yes, the value of x as textYesAt the start of the next line
System.out.println()No visible textYesAt the start of the next line

How the three output operations differ in what they display and where the next output begins

Following a Sequence of Output Calls

When a program contains several output statements, the console shows the result of each statement in order. Trace the sequence from top to bottom, and mark each operation that adds a line feed. Those marks tell you exactly where each new line begins. The method below follows that habit on a four-statement program.

Worked example

Tracing print, print, println, and print

What does the console show after each statement of the following program runs in order? System.out.print("A"); System.out.print("B"); System.out.println(); System.out.print("C");

  1. 1
    Statement 1: print A

    A is displayed. Print adds no line feed, so the position stays right after A. The console shows A.

  2. 2
    Statement 2: print B

    B is displayed right after A. The position stays after B. The console shows AB on one line.

  3. 3
    Statement 3: println with no argument

    No visible text is displayed. The call adds only a line feed, so the position moves to the start of the next line. The text AB is now complete on its line.

  4. 4
    Statement 4: print C

    C is displayed at the start of the new line, because the previous line feed moved the position there. The console shows AB on the first line and C on the second line.

Result

The final console shows AB on the first line and C on the second line.

Statement 1print A: console AStatement 2print B: console ABStatement 3println(): AB, then linefeedStatement 4print C: C on new linenextnextnext
After each statement runs in order, what does the console show, and at which point does a new line begin?

Notice that statement 3 displays nothing of its own. Its entire contribution is the line feed. Without it, C would have appeared right after B on the same line, which is exactly the kind of unexpected placement this trace is designed to catch.

Using Println With No Argument

The no-argument form System.out.println() outputs no visible text. Its only purpose is to add a line feed. This makes it useful for separating text produced before it from text produced afterward, without displaying anything new. The next visual shows the same starting point before and after one such call.

Before println()A, cursor on same lineAfter println()A, cursor on next lineprintln()
What does the console look like when println() is called with no argument? Only the position changes, not the text.

Mistakes When Tracing Output

  • Common mistake

    Assuming print adds a line break after its text

    What it looks like
    System.out.print("A"); System.out.print("B"); displays AB, not A and B on separate lines.
    Why it fails

    Print displays its text and stops there. The output position stays after the displayed text.

    Do this instead

    Use println when the next output should begin on a new line, or add a println() call between the two statements.

  • Common mistake

    Ignoring statements that display no visible text

    What it looks like

    In the sequence print A, print B, println(), print C, a reader may expect C to appear beside B.

    Why it fails

    The no-argument println() adds a line feed even though it displays nothing, so C starts on a new line.

    Do this instead

    Mark every operation that adds a line feed as you trace, including println() with no argument.

  • Common mistake

    Looking only at the displayed values when the layout is wrong

    What it looks like

    A program shows the right words, but they appear on the wrong lines.

    Why it fails

    Each output operation both displays text and decides where later text begins, so the layout problem may come from a line feed rather than from the values.

    Do this instead

    Trace the output statements in order, and identify where println or an empty println changes the position.

Predicting the Console Lines

MEDIUM

Without running the code, predict the console output of this program, then check your answer by tracing each statement in order: System.out.print("Hi "); System.out.println("there"); System.out.print("friend"); System.out.println();

Hints
  • Start with the first statement. Print displays Hi and a space, and the position stays after the space.
  • The second statement displays there and then adds a line feed. Where does the line end?
  • The third statement starts on a new line. Does the final println() add any visible text?

What do you think happens?

Which lines does the console show for the program in the challenge above?

  • Hi there on line one, and friend on line two

  • Hi there friend on a single line

  • Hi on line one, there on line two, and friend on line three

Reveal answer
Answer:

Hi there on line one, and friend on line two

The first print leaves the position after Hi and a space. The println displays there and adds a line feed. The next print displays friend on the new line, and the final println() adds only a line feed, which does not create a visible extra line of text.

Key Takeaways

  • System.out.print(x) outputs x after converting it to a string when necessary, and it does not add a line feed, so later output continues on the same line.
  • System.out.println(x) outputs the same text as print and then adds a line feed, so later output begins on the next line.
  • System.out.println() with no argument displays no visible text; its only effect is to add a line feed.
  • To debug unexpected layout, trace the output statements in order and mark each operation that adds a line feed.