Description
The char type is a 16-bit unsigned integer
representing Unicode characters. The default value of a char instance
variable, class variable, and array component is the null character, 0.
The value of char is an integer from 0 to 65535 inclusive.
A character literal is expressed as a character or escape sequence, enclosed in ASCII single quotes. The following table shows the escape sequences.
Escape Sequence | Unicode Value | Description |
\b | \u0008 | backspace |
\t | \u0009 | horizontal tab HT |
\n | \u000a | linefeed LF |
\f | \u000c | form feed FF |
\r | \u000d | carriage return CR |
\" | \u0022 | double quote |
\' | \u0027 | single quote |
\\ | \u005c | backslash |
Because Unicode escapes are processed very early, it is not correct to write '\u000a' for a character literal whose value is linefeed (LF); the Unicode escape \u000a is transformed into an actual linefeed in the translation of a source file into a class file. The linefeed becomes a LineTerminator, and so the character literal is not valid. Instead, you should use the escape sequence '\n'.
Examples
The following examples show legal and illegal character variable definitions.
char capitalA = 'A'; // the capital A literal char LF = '\n'; // the linefeed escape sequence char otherCapitalA = '\u0041'; // the Unicode value of a capital A char capitalB = capitalA++; // you can use integer operations on chars char badChar = 100000; // ILLEGAL. The value is greater than 65535.
Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.