Java Special Characters and Escape Sequences
Use Java escape sequences such as \n, \t, \\, \", and Unicode escapes inside string literals.
Some characters can't be written directly inside a string literal — a " would end the string, a literal newline would break the source line, and a backslash already has a special job: it tells the compiler "treat what follows as an escape." Java solves this with escape sequences: short, backslash-prefixed codes that stand in for those awkward characters. A separate mechanism, Unicode escapes (\uXXXX), lets you write any code point in the Basic Multilingual Plane by its hex value.
This page covers the standard escape sequences, escapes inside char literals, Unicode and octal escapes, how text blocks relax the rules, and the mistakes that trip people up most often.
The standard escape sequences
| Sequence | Meaning |
|---|---|
\n | newline (LF, U+000A) |
\r | carriage return (CR, U+000D) |
\t | tab (U+0009) |
\b | backspace (U+0008) |
\f | form feed (U+000C) |
\" | double quote |
\' | single quote |
\\ | a single backslash |
\0 | null character (U+0000) |
\s | space (U+0020), added in Java 15 |
Examples:
String multi = "Line 1\nLine 2\nLine 3";
String quoted = "She said \"hi\"";
String tabbed = "name\tage\tcity";
String path = "C:\\Users\\Ada\\code.java";When you print these, the escape sequences become the actual characters:
Line 1
Line 2
Line 3
She said "hi"
name age city
C:\Users\Ada\code.javaInside a char literal
A char literal is enclosed in single quotes. The same escape sequences apply, but the rules flip for quotes: inside single quotes you must escape \', while " may be written plainly. (Inside a double-quoted string it's the reverse — \" is escaped and ' is plain.)
char quote = '\''; // a single-quote character
char tab = '\t'; // a tab
char back = '\\'; // one backslash
char dquote = '"'; // no escape needed hereUnicode escapes
To embed any Basic Multilingual Plane character, use \uXXXX where XXXX is the 4-digit hex code point:
String greeting = "Café"; // "Café"
String pi = "π ≈ 3.14"; // "π ≈ 3.14"
char heart = '♥'; // '♥'Code points outside the BMP (most emoji) need a surrogate pair — two \uXXXX escapes — so it's almost always easier to paste the character directly.
\uXXXX in a very first pass, before the source is even broken into tokens. That's why a stray (newline) inside a string is a compile error rather than a runtime newline, and why \uXXXX is valid anywhere — comments, identifiers, even between operators. Ordinary escapes like \n and \t are only interpreted later, inside string and char literals.Octal escapes
\ followed by 1–3 octal digits (0–7) is the character with that octal value:
char c = '\101'; // 'A' (decimal 65)
char d = '\14'; // form feedYou'll rarely see these — Unicode escapes are clearer.
Raw text blocks bypass most escapes
In a text block (Java 15+), you can write newlines and unescaped double quotes literally — perfect for embedding JSON, SQL, or HTML. The only sequences you still need are \\ for a literal backslash and \uXXXX for Unicode:
String json = """
{
"name": "Ada",
"tagline": "She invented programming"
}
""";Notice the "name" and "tagline" keys don't need \" — they sit inside a """...""" block.
A demonstration
Common mistakes
- Writing
"\n"and expecting Windows line endings.\nis LF only. For platform-correct newlines in formatted output, use%ninsideprintf/format— it expands to the host's line separator. - Forgetting to escape backslashes in regex. A regex pattern lives inside a Java string first, so it goes through two layers of escaping. To match a digit (
\d) you write the string"\\d"— the compiler turns\\into one backslash, and the regex engine then reads\d. To match a literal backslash you need four:"\\\\". - Using
to embed a newline.is the newline code point, and because Unicode escapes are translated before the tokenizer runs, the compiler substitutes a real line break in the middle of your string literal — which is a syntax error. Use\nfor newlines inside strings; reserve\uXXXXfor printable characters. - An incomplete or invalid Unicode escape.
\umust be followed by exactly four hex digits."\u12"or"\uZZZZ"is a compile error, not a literal backslash. If you want a literal\u, escape the backslash:"\\u".
What's next
Java Numbers takes a focused look at the numeric types, their literals, and their precision.