Basic Output and Formatted Output
System.out.print(x) outputs x after converting it to a string when necessary.
Why Text Sometimes Lands on the Wrong Line
When a Java program prints text and the result looks wrong, the value being displayed is often not the problem. The real issue is frequently 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 statement at a time.
What do you think happens?
After System.out.print("A") runs and then System.out.print("B") runs, what appears on the console?
Reveal answer
AB on one line
Plain print sends its text and leaves the cursor where it is, so the next print continues on the same line. Only println adds a line feed that moves future output to a new line.
Tracing the Cursor After Each Call
The difference between print and println is easiest to see by following the cursor. Think of the cursor as the position where the next character will appear. A print call writes its text at the cursor and leaves the cursor just after that text. A println call writes its text the same way, and then adds a line feed, which moves the cursor to the start of the next line.
In the first case, both pieces of text sit on the same line because neither call ends with a line feed. In the second case, the line feed added by println("A") pushes the B onto a new line. The text displayed is identical in both cases; only the position of the cursor after the first call has changed.
What print and println Each Do
System.out.print(x) outputs x after converting it to a string when necessary. System.out.println(x) outputs the same text as print and then adds a line feed.
The two methods share the same display behavior. The only difference is what happens after the text is written. This means you can decide the layout of output by choosing between them: print when the next piece of text should continue on the same line, and println when the next piece of text should begin a new line.
Hello, world!
DoneTrace the example line by line. The first two print calls place Hello and , world on the same line without any line feed. The println call writes ! and then adds a line feed, so the cursor moves down. The last print call writes Done at the start of the new line, and nothing follows it, so no line feed is added after it.
A print call leaves the cursor after its text. A println call leaves the cursor at the start of the next line. Predicting the next line position requires knowing which of these two calls ran last.
Printing Values and Expressions
print does not only accept text. It accepts values and expressions too. When the argument is not already a string, print converts it to a string before the characters reach the console. The source describes this as converting x to a string when necessary, so a number such as 7 is shown as the characters 7, and an expression such as 3 + 4 is evaluated first and then its result is displayed.
Sum: 7- The string Sum: is printed as written, including the trailing space.
- 3 + 4 is evaluated to 7 before printing.
- The value 7 is converted to characters and written right after Sum: on the same line.
The Line Feed Alone
System.out.println() with no argument outputs no visible text. Its only purpose is to add a line feed. This makes it a tool for controlling layout: it ends the current line without adding any characters to it, so the next output starts on a fresh line.
First
Secondprint("First")writes First and leaves the cursor after it.println()writes nothing but adds a line feed, ending the line that contains First.print("Second")starts at the beginning of the next line.
Without the empty println call, the word Second would appear directly after First on the same line. The empty call separates the text produced before it from the text produced afterward, and it does so without introducing any extra characters.
Tracing a Full Output Sequence
When a program has several output statements, trace them in order. For each statement, ask two questions: what text does it display, and does it end with a line feed? The diagram below follows four statements and shows where each piece of text lands and where the cursor is left for the next call.
Predicting the Lines of a Mixed Sequence
Given the statements print Red, println with no argument, print Blue, println Green, what lines appear on the console?
- 1Statement 1
print Red writes Red on line 1 and leaves the cursor after it, so the next output continues on line 1.
- 2Statement 2
println with no argument writes no text but adds a line feed. Line 1 is now complete and the cursor moves to line 2.
- 3Statement 3
print Blue writes Blue at the start of line 2 and leaves the cursor after it.
- 4Statement 4
println Green writes Green directly after Blue on line 2, then adds a line feed. Both words share line 2 because the text is written before the line feed.
Line 1: Red. Line 2: BlueGreen.
Mistakes Beginners Make with Line Placement
Common mistake Assuming print adds a line break after its text
What it looks likeSystem.out.print("A"); System.out.print("B");Why it failsprint displays its text and leaves the cursor where it is, so B continues on the same line as A.
Do this insteadUse println("A") if the next output should begin on a new line, or add System.out.println() between the two calls.
Common mistake Forgetting that println(x) adds a line feed after x
What it looks likeSystem.out.println("Total"); System.out.print("42");Why it failsThe line feed from println moves the cursor down after Total, so 42 begins on the next line. Beginners sometimes expect 42 to follow Total directly.
Do this insteadUse print("Total: ") and then print(42) when the number should stay on the same line as its label.
Common mistake Expecting println() with no argument to display a blank value
What it looks likeSystem.out.println();Why it failsThe no-argument form outputs no visible text. Its only effect is the line feed.
Do this insteadTreat println() as a pure line break, and do not expect it to display anything.
Common mistake Blaming the displayed value when the layout is wrong
Why it failsThe text itself may be correct while the next piece lands on the wrong line, because the earlier call ended with or without a line feed.
When output is in the wrong position, locate the last output operation before the unexpected position. If it was print, later text can remain on the same line. If it was println, later text starts after the line feed it added. Mark each operation that adds a line feed as you trace the program.
Where Standard Output Goes
In the programs covered here, standard output is generally the place where a user sees program text. Standard output is not permanently fixed to one visible screen, however. It can be redirected to another destination, and in a GUI program it may appear somewhere a typical user does not see.
Predicting Your Own Output
Without running the code, predict the exact console output of these three statements: System.out.print("X"); then System.out.println("Y"); then System.out.print("Z"); Write down which characters appear on line 1 and which appear on line 2 before you check your answer.
Hints
- Ask after each statement whether the cursor stays on the same line or moves down.
- The println call is the only one that adds a line feed.
- Z is printed after the line feed, so it starts on the line below Y.
Key Takeaways
System.out.print(x)outputs x after converting it to a string when necessary, and leaves the cursor after the text.System.out.println(x)outputs the same text as print and then adds a line feed, so later output begins on a new line.System.out.println()with no argument outputs no visible text; it only adds a line feed.- To predict or debug line placement, trace each output statement in order and note which ones end with a line feed.
- The print-versus-println rule applies to standard output wherever that output is directed.