Home > Articles

  • Print
  • + Share This

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 0 and 256 with the upper byte set to 0).

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 0 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).

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.