Use the backslash character to apply escaped constructs and to quote characters that are interpreted as unescaped constructs. For example, the expression
\\
matches a single backslash, and the expression
\{
matches a left brace.
Java interprets backslashes that are used as string literals as Unicode or character escapes. To prevent the Java byte code compiler from interpreting them, use a double backslash in string literals that use regular expressions.
For example, the string literal
\b
matches a single backspace character when interpreted as a regular expression. However,
\\b
matches a word boundary. The string literal
\(hello\)
is illegal and results in a compile-time error. To match the string "(hello)," use the string literal
\\(hello\\)
.
Do not use a backslash before any alphabetic character that does not denote an escaped construct. Use a backslash before a character if the character is not alphabetic and not part of an unescaped construct.