Character Literals

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:
UnicodeInputCharacter but not ' or \

The escape sequences are described in Escape Character Literals.

The characters CR and LF are never an InputCharacter; they are recognized as constituting a LineTerminator.

ou will receive a parse-time error if the character following the SingleCharacter or EscapeSequence is other than a '. You will also receive a parse-time error if a line terminator appears after the opening ' and before the closing '.

The following are examples of char literals:

'a'
'%'
'\t'
'\\'
'\''
'\u03a9'
'\uFFFF'
'\177'
' '
' '

Each char literal is a reference to an instance of class Character. These objects have a constant value and can be used interchangeably with its counter part Java primitive data type when calling methods that expect the primitive types or when accessing Java attributes declared using the Java primitive data type.