Java Reference
In-Depth Information
9
System.out.println(
"Welcome to Java Programming!"
);
\n \n
\n
10
}
// end method main
11
}
// end class Welcome3
Welcome
to
Java
Programming!
Fig. 2.4
|
Printing multiple lines of text with a single statement. (Part 2 of 2.)
Line 9
System.out.println(
"Welcome\nto\nJava\nProgramming!")
;
displays four lines of text in the command window. Normally, the characters in a string are
displayed
exactly
as they appear in the double quotes. However, the paired characters
\
and
n
(repeated three times in the statement) do
not
appear on the screen. The
backslash
(
\
) is
an
escape character
, which has special meaning to
System.out
's
print
and
println
meth-
ods. When a backslash appears in a string, Java combines it with the next character to form
an
escape sequence
—
\n
represents the newline character. When a newline character appears
in a string being output with
System.out
, the newline character causes the screen's output
cursor to move to the beginning of the next line in the command window.
Figure 2.5 lists several common escape sequences and describes how they affect the dis-
play of characters in the command window. For the complete list of escape sequences, visit
Escape
sequence
Description
\n
Newline. Position the screen cursor at the beginning of the
next
line.
\t
Horizontal tab. Move the screen cursor to the next tab stop.
\r
Carriage return. Position the screen cursor at the beginning of the
current
line—do
not
advance to the next line. Any characters output after the car-
riage return
overwrite
the characters previously output on that line.
Backslash. Used to print a backslash character.
\\
\"
Double quote. Used to print a double-quote character. For example,
System.out.println(
"\"in quotes\""
);
displays
"in quotes"
.
Fig. 2.5
|
Some common escape sequences.
The
System.out.printf
method (
f
means “formatted”) displays
formatted
data.
Figure 2.6 uses this method to output on two lines the strings
"Welcome
to"
and
"Java
Programming!"
.