Text Input and Output Fundamentals
System.out.print(x) outputs x after converting it to a string when necessary.
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");
Reveal 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.
AB- 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.
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
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?
A
BC- 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.
| Statement | Text displayed | Line feed added | Where later output begins |
|---|---|---|---|
| System.out.print(x) | Yes, the value of x as text | No | Right after the displayed text, on the same line |
| System.out.println(x) | Yes, the value of x as text | Yes | At the start of the next line |
| System.out.println() | No visible text | Yes | At 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.
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");
- 1Statement 1: print A
A is displayed. Print adds no line feed, so the position stays right after A. The console shows A.
- 2Statement 2: print B
B is displayed right after A. The position stays after B. The console shows AB on one line.
- 3Statement 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.
- 4Statement 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.
The final console shows AB on the first line and C on the second line.
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.
Mistakes When Tracing Output
Common mistake Assuming print adds a line break after its text
What it looks likeSystem.out.print("A"); System.out.print("B"); displays AB, not A and B on separate lines.Why it failsPrint displays its text and stops there. The output position stays after the displayed text.
Do this insteadUse 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 likeIn the sequence print A, print B, println(), print C, a reader may expect C to appear beside B.
Why it failsThe no-argument
println()adds a line feed even though it displays nothing, so C starts on a new line.Do this insteadMark 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 likeA program shows the right words, but they appear on the wrong lines.
Why it failsEach 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 insteadTrace the output statements in order, and identify where println or an empty println changes the position.
Predicting the Console Lines
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?
Reveal 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.