Concepts / Basic Output and Formatted Output

Basic Output and Formatted Output

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

  • Programming

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?

  • AB on one line

  • A and B on two separate lines

Reveal answer
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.

ABprint then printAprintln then printBnext lineline feed
After print A and then print B, does B share a line with A? After println A and then print B, what changes?

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

Definition

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.

java
Output
Hello, world!
Done

Trace 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.

Key point

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.

java
Output
Sum: 7
Notes on the code
  • 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.

java
Output
First
Second
Notes on the code
  • print("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.

print Hicursor stays on line 1print thereHi there on line 1printlnline feed addedprint NextNext starts line 2thenthenthen
Where does each piece of text land, and where is the cursor left for the next call?
Worked example

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?

  1. 1
    Statement 1

    print Red writes Red on line 1 and leaves the cursor after it, so the next output continues on line 1.

  2. 2
    Statement 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.

  3. 3
    Statement 3

    print Blue writes Blue at the start of line 2 and leaves the cursor after it.

  4. 4
    Statement 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.

Result

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 like
    System.out.print("A");
    System.out.print("B");
    Why it fails

    print displays its text and leaves the cursor where it is, so B continues on the same line as A.

    Do this instead

    Use 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 like
    System.out.println("Total");
    System.out.print("42");
    Why it fails

    The 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 instead

    Use 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 like
    System.out.println();
    Why it fails

    The no-argument form outputs no visible text. Its only effect is the line feed.

    Do this instead

    Treat 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 fails

    The 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.

Best practice

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

EASY

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.