String and character literals provide an escape mechanism that allows express character codes that would otherwise not be allowed in the literal. An escape sequence consists of a backslash character (
\) followed by one ore more other characters. The same sequences are valid in both character an string literals.
The complete set of escape sequences is as follows:
|Denotes an backslash (|
|Denotes a single-quote (|
|Denotes a double-quote (|
|Denotes a line feed (|
|Denotes a carriage return (|
|Denotes a horizontal tab (|
|Denotes a form feed (|
|Denotes a backspace (|
|Denotes a character code in the range 0 to 255.|
<octal> in the above consists of one, two or three octal digits ('0' through '7') which represent a number between 0 and 255 (decimal).
Note that a backslash followed by any other character is an invalid escape sequence. Invalid escape sequences are treated as compilation errors by the JLS.
In addition to the string and character escape sequences described above, Java has a more general Unicode escaping mechanism, as defined in JLS 3.3. Unicode Escapes. A Unicode escape has the following syntax:
'\' 'u' <hex-digit> <hex-digit> <hex-digit> <hex-digit>
<hex-digit> is one of
A Unicode escape is mapped by the Java compiler to a character (strictly speaking a 16-bit Unicode code unit), and can be used anywhere in the source code where the mapped character is valid. It is commonly used in character and string literals when you need to represent a non-ASCII character in a literal.