A character literal is expressed as a character or an escape sequence, enclosed in
ASCII single quotes. (The single-quote, or apostrophe, character is \u0027
.)
A character literal is always of type char
.
CharacterLiteral:
'
SingleCharacter'
EscapeSequence
''
SingleCharacter:
InputCharacter but not'
or\
The escape sequences are described in §3.10.6.
As specified in §3.4, the characters CR and LF are never an InputCharacter; they are recognized as constituting a LineTerminator.
It is a compile-time error for the character following the SingleCharacter or EscapeSequence to be other than a '
.
It is a compile-time error for a line terminator to appear after the opening '
and before the closing '
.
The following are examples of char
literals:
'a' '%' '\t' '\\' '\'' '\u03a9' '\uFFFF' '\177' '' ''
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 translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n'
(§3.10.6). Similarly, it is not correct to write '\u000d'
for a character literal whose value is carriage return (CR). Instead, use '\r'
.
In C and C++, a character literal may contain representations of more than one character, but the value of such a character literal is implementation-defined. In Java, a character literal always represents exactly one character.