Sams Teach Yourself Java™ in 24 Hours (Covering Java 7 and Android)

(singke) #1
ptg7068951

Using Special Characters in Strings 67

You can use System.out.print()several times in a row to display several
things on the same line, as in this example:


System.out.print(“She “);
System.out.print(“never “);
System.out.print(“said “);
System.out.print(“another “);
System.out.println(“word.”);


These statements cause the following text to be displayed:


She never said another word.


Using Special Characters in Strings


When a string is being created or displayed, its text must be enclosed with-
in double quotation marks. These quotation marks are not displayed,
which brings up a good question: What if you want to display double
quotation marks?


To display them, Java has created a special code that can be put into a
string: \”. Whenever this code is encountered in a string, it is replaced with
a double quotation mark. For example, examine the following:


System.out.println(“Jane Campion directed \”The Piano\” in 1993.”);


This code is displayed as the following:


Jane Campion directed “The Piano” in 1993.


You can insert several special characters into a string in this manner. The
following list shows these special characters; note that each is preceded by
a backslash ().


Special Characters Display


\’ Single quotation mark


\” Double quotation mark


\ Backslash


\t Ta b


\b Backspace


\r Carriage return


\f Formfeed


\n Newline

Free download pdf