Variables of the type char store a single character. They each occupy 16 bits, two bytes, in memory because all characters in Java are stored as Unicode. To declare and initialize a character variable myCharacter you would use the statement:
char myCharacter = 'X';
This initializes the variable with the Unicode character representation of the letter 'X'. You must put single quotes around a character in a statement – 'X'. This is necessary to enable the compiler to distinguish between the character 'X' and a variable with the name X. Note that you can't use double quotes here as they are used to delimit a character string. A character string such as "X" is quite different from the literal of type char, 'X'.
If you are using an ASCII text editor you will only be able to enter characters directly that are defined within ASCII. You can define Unicode characters by specifying the hexadecimal representation of the character codes in an escape sequence. An escape sequence is simply an alternative means of specifying a character, often by its code. A backslash indicates the start of an escape sequence, and you create an escape sequence for a Unicode character by preceding the four hexadecimal digits of the character by \u. Since the Unicode coding for the letter X is 0x0058 (the low order byte is the same as the ASCII code), you could also declare and define myCharacter with the statement:
char myCharacter = '\u0058';
You can enter any Unicode character in this way, although it is not exactly user-friendly for entering a lot of characters.
You can get more information on the full Unicode character set on the Internet by visiting http://www.unicode.org/.
As you have seen, we can write a character string (a String literal as we will see in Chapter 4) enclosed between double quotes. Because the backslash indicates the beginning of an escape sequence in a character string, you must use an escape sequence to specify a backslash character itself in text strings, \\. Since a single quote is used to delimit characters, and we use a double quote to delimit a text string, we also need escape sequences for these. You can define a single quote with the escape sequence \', and a double quote with \". For example, to produce the output:
"It's freezing in here", he said coldly.
you could write:
In fact, it's not strictly necessary to use an escape sequence to specify a single quote within a string, but obviously it will be when you want to specify it as a single character. Of course, it is always necessary to specify a double quote within a string using an escape sequence otherwise it would be interpreted as the end of the string.
There are other escape characters you can use to define control characters:
You can perform arithmetic on char variables. With myCharacter containing the character 'X', the statement:
myCharacter += 1; // Increment to next character
will result in the value of myCharacter being changed to 'Y'. This is because the Unicode code for 'Y' is one more than the code for 'X'. You could use the increment operator ++ to increase the code stored in myCharacter by just writing:
++myCharacter; // Increment to next character
You can use variables of type char in an arithmetic expression, and their values will be converted to type int to carry out the calculation. It doesn't necessarily make a whole lot of sense, but you could write,
char aChar = 0; char bChar = '\u0028'; aChar = (char)(2*bChar + 8);
which will leave aChar holding the code for X – which is 0x0058.