The char Data Type

The char data type is used to represent a single character. In Java, characters are stored using 2 bytes of memory. The reason for this is that Java allows for the storage of many more characters than just the ASCII character set, which only contains 256 individual characters. Java uses a 16-bit character set called Unicode, which is the worldwide character encoding standard. Ideally, the low-order byte of the Unicode representation can be used to store the ASCII representation of characters, which means that a character with a value from 0 to 255 will be an ASCII character. The following line of code shows the declaration of char variables.

char firstLowercaseLetter = 'a';
char firstUppercaseLetter = 'A';
char ampersand = '&';

As you can see, the character must be specified between single quotation characters. A char is effectively the same as a short in that it is the same size and can be assigned its value numerically, which can then be altered with an arithmetic expression. For example, the ASCII numeric value of the letter "A" is 65, which means that the following line of code will also assign the character value "A" to a char variable and then increment the value by one, giving the variable the value of 66, which is the value of the letter "B" character.

char letter = 65; // value equals 'A'
letter++; // next value is 'B'