Escape sequences in literals

suggest change

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:

Escape sequence | Meaning | —— | —— |\\ | Denotes an backslash (\\) character |\' | Denotes a single-quote (\') character |\" | Denotes a double-quote (") character |\n | Denotes a line feed (LF) character |\r | Denotes a carriage return (CR) character |\t | Denotes a horizontal tab (HT) character |\f | Denotes a form feed (FF) character |\b | Denotes a backspace (BS) character |\<octal> | Denotes a character code in the range 0 to 255. |

The <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.

Reference:

Unicode escapes

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>

where <hex-digit> is one of '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'A', 'B', 'C', 'D', 'E', 'F'.

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.

Escaping in regexes

TBD

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Literals:
* Escape sequences in literals

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap