Character Variables

   

Character Variables

A feature of Java that makes it unique is its built-in support for internationalization (see Chapter 24, "Using Internationalization" ). A key component of this support is the storage of characters as 16-bit values rather than a typical 8-bit representation. The char primitive type holds a 16-bit Unicode character. The Unicode standard allows the use of languages with alphabets that cannot be represented with only 256 characters. Although different from what you might be accustomed to, Java's representation of characters is transparent to you as a programmer. As a convenience, char variables that represent the Latin alphabet, numerals, and punctuation symbols have the same values as the ASCII character set (these characters have values between and 256 with the upper byte set to ).

The syntax to create a character variable is the same as for the other primitives:

 char myChar = 'b'; 

In this example, the myChar variable has been assigned the value of the letter 'b'. Character literals must be expressed within single quotes as shown here.

Variables of type char can also be converted to the integer primitive types. When viewed as an integer, a char can hold values between and 65,535. It is unique in that its values are treated as unsigned (a negative index into a character set wouldn't be too meaningful).

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net