Java Reference
In-Depth Information
print versus println
Java has a variation of the println command called print that allows you to produce
output on the current line without going to a new line of output. The println com-
mand really does two different things: It sends output to the current line, and then it
moves to the beginning of a new line. The print command does only the first of
these. Thus, a series of print commands will generate output all on the same line.
Only a println command will cause the current line to be completed and a new line
to be started. For example, consider these six statements:
System.out.print("To be ");
System.out.print("or not to be.");
System.out.print("That is ");
System.out.println("the question.");
System.out.print("This is");
System.out.println(" for the whole family!");
These statements produce two lines of output. Remember that every println
statement produces exactly one line of output; because there are two println state-
ments here, there are two lines of output. After the first statement executes, the cur-
rent line looks like this:
To be
^
The arrow below the output line indicates the position where output will be sent
next. We can simplify our discussion if we refer to the arrow as the output cursor.
Notice that the output cursor is at the end of this line and that it appears after a space.
The reason is that the command was a print (don't go to a new line) and the string
literal in the print ended with a space. Java will not insert a space for you unless
you specifically request it. After the next print , the line looks like this:
To be or not to be.
^
There's no space at the end now because the string literal in the second print
command ends in a period, not a space. After the next print , the line looks like this:
To be or not to be.That is
^
There is no space between the period and the word “That” because there was no
space in the print commands, but there is a space at the end of the string literal in
the third statement. After the next statement executes, the output looks like this:
To be or not to be.That is the question.
^
 
Search WWH ::




Custom Search