Java Reference
In-Depth Information
char myCharacter = '\u0058';
You place the escape sequence between single quotes to define the character literal. The result is the
same as the previous statement where you used 'X' as the initial value for myCharacter . You can enter any
Unicode character in this way, as long as you know its code of course.
NOTE You can get more information on the full Unicode character set on the Internet by vis-
iting www.unicode.org/ .
Because the backslash indicates the beginning of an escape sequence, you must always use the escape
sequence, \\ , to specify a backslash character as a character literal or in a text string.
As you have seen, you write a character string (a String literal as shown in Chapter 4) enclosed between
double quotes and a character literal between single quotes. For this reason you also need the escape se-
quences \' and \" to specify these characters. For example, to produce the output
"It's freezing in here", he said coldly.
you could write
System.out.println("\"It\'s freezing in here\", he said coldly.");
In fact, it's not strictly necessary to use an escape sequence to specify a single quote within a string, but
obviously it is when you want to specify a single quote as a character literal. Of course, it is always neces-
sary to specify a double quote within a string using an escape sequence, otherwise it would be interpreted as
the end of the string.
There are other escape sequences that you use to define control characters (shown in Table 2-6 ) .
TABLE 2-6 : Escape Sequences
CHARACTER DESCRIPTION
\b
Backspace
Form feed
\f
New line
\n
Carriage return
\r
Tab
\t
Character Arithmetic
You can perform arithmetic on char variables. With myCharacter containing the character 'X' , the state-
ment
myCharacter += 1; // Increment to next character
 
 
Search WWH ::




Custom Search