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 code, and a backslash means "what follows is an escape." Java uses escape sequences to represent these characters, plus Unicode escapes for any code point in the Basic Multilingual Plane.
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 (Java 15+, in text blocks mostly) |
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. You'll need \' to put a literal single quote inside one:
char quote = '\'';
char tab = '\t';
char back = '\\';Unicode 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 = '♥'; // '♥'Unicode escapes are processed by the compiler before anything else, so they work anywhere in the source — even inside identifiers (don't actually do that — keep your code readable).
Code points outside the BMP (most emoji) need a surrogate pair or the \N{name} syntax — easier to just paste the character directly.
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, tabs, and double quotes literally. The only sequences you still need to escape are \\ and Unicode escapes:
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. - Forgetting to escape backslashes in regex. Inside a Java string, regex
\dis written"\\d"— two characters: a backslash and ad, which the regex engine sees as\d. - Using
\uto embed a newline.is a newline — and because Unicode escapes are processed before the tokenizer, it breaks your source line in half. Use\nfor newlines in strings.
What's next
Java Numbers takes a focused look at the numeric types, their literals, and their precision.
Practice
Which escape sequence represents a single backslash inside a string literal?