Java Reference
In-Depth Information
8 JOptionPane.INFORMATION_MESSAGE);
9 }
10 }
If no Chinese font is installed on your system, you will not be able to see the Chinese char-
acters. The Unicodes for the Greek letters are \u03b1 \u03b2 \u03b3 .
Most computers use ASCII (American Standard Code for Information Interchange) , a 7-bit
encoding scheme for representing all uppercase and lowercase letters, digits, punctuation marks,
and control characters. Unicode includes ASCII code, with \u0000 to \u007F corresponding
to the 128 ASCII characters. (See Appendix B for a list of ASCII characters and their decimal
and hexadecimal codes.) You can use ASCII characters such as 'X' , '1' , and '$' in a Java
program as well as Unicodes. Thus, for example, the following statements are equivalent:
abg
ASCII
char letter = 'A' ;
char letter = '\u0041' ; // Character A's Unicode is 0041
Both statements assign character A to the char variable letter .
Note
The increment and decrement operators can also be used on char variables to get the
next or preceding Unicode character. For example, the following statements display
character b .
char ch = 'a' ;
System.out.println(++ch);
char increment and
decrement
2.17.2 Escape Characters
Suppose you want to print a message with quotation marks in the output. Can you write a
statement like this?
System.out.println( "He said "Java is fun"" );
No, this statement has a compile error. The compiler thinks the second quotation character
is the end of the string and does not know what to do with the rest of the characters.
To overcome this problem, Java uses a special notation to represent special characters, as
shown in Table 2.6. This special notation, called an escape character , consists of a backslash
( \ ) followed by a character or a character sequence. For example, \t is an escape character
for the Tab character and an escape character such as \u03b1 is used to represent a Unicode.
The symbols in an escape character are interpreted as a whole rather than individually .
So, now you can print the quoted message using the following statement:
escape character
System.out.println( "He said \"Java is fun\"" );
The output is
He said "Java is fun"
Note that the symbols \ and " together represent one character.
2.17.3 Casting between char and Numeric Types
A char can be cast into any numeric type, and vice versa. When an integer is cast into a
char , only its lower 16 bits of data are used; the other part is ignored. For example:
char ch = ( char ) 0XAB0041 ; // The lower 16 bits hex code 0041 is
// assigned to ch
System.out.println(ch);
// ch is character A
 
Search WWH ::




Custom Search