contains the characters 'A' , ' ;' , and 'a' . If you understand the char literals, you
know what you can use to make String literals.
How do you write the character ' in a Java program? You cannot write ''' .
Instead, write '\'' . The backslash \ is called the escape character , and the
sequence \' is the escape sequence that is used to denote a single quote. Figure
6.1 lists the more important escape sequences that you can use in Java.
There is a third kind of char literal, the unicode character, like '\u0041' .
Unicode contains representations of almost all alphabets of the world. You do not
have to know about Unicode now, and we do not describe it here. For informa-
tion on it and the older 8-bit ASCII character-set representation, look at the CD.
There are no operations on type char (other than casts).
the ASCII and
Type char is an integral type
Type char is actually an integral type, which means that one can convert a
value of type char into an integer and back again. Recall from Sec. 6.3 the dia-
gram concerning narrower and wider integral types:
byte short int long
Below, we show the same kind of diagram, which includes type char :
char int long
Thus, char is narrower than int .
If you cast a char value to int , you get the integer that represents it. If you
cast an int value to char , you get the char that it represents. For example:
( int ) 'A' equals 65
( char )65 equals 'A'
Since char is an integral type, it is easy to write loops that sequence and process
a range of characters, for example, all the lower-case letters 'a'..'z' . The CD
contains activities that show you how to do this.
Casting among primitive types
In Sec. 6.3, we introduced casting among integral types. In Sec. 6.5, we dis-
cussed the fact that type char was an integral type and showed how to cast from
char to int and back. In this section, we complete the discussion on casting by