Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Text, Formatting and Regex

Java escape sequences

Reference for Java escape sequences in string and char literals: \n, \t, \s, \", octal escapes, \uXXXX Unicode escapes and their surprising rules.

An escape sequence is a backslash followed by one or more characters, written inside a char or String literal to produce a character that is hard or impossible to type directly: a new line, a tab, a double quote, a character outside your keyboard. The compiler replaces each sequence by the character it stands for.

Java has two different mechanisms with the same look: ordinary escape sequences like \n and \", which are handled inside string and character literals, and Unicode escapes like \u0041, which are translated very early, before the code is even tokenized. The second kind causes most of the surprises.

Java
String s = "Name:\tAnn\nQuote: \"hi\"";
char apostrophe = '\'';

Try it

Java Escapes and their character codes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Escapes in practice

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Escape Character Code Notes
\n line feed 10 newline on Unix and in text blocks
\r carriage return 13 with \n forms the Windows line break
\t horizontal tab 9
\b backspace 8
\f form feed 12
\s space 32 Java 15; survives trailing-space stripping in text blocks
\" double quote 34 needed in "..." strings
\' single quote 39 needed in '...' char literals
\\ backslash 92
\0 to \377 octal value 0 to 255 one to three octal digits; first digit at most 3 when three are used
\<line terminator> nothing none text blocks only (Java 15): joins lines
\uXXXX UTF-16 code unit 0 to 65535 exactly four hex digits; one or more u letters allowed

An unknown escape such as \d or \q is a compile error. Characters beyond U+FFFF are written as two escapes for the surrogate pair ("\uD83D\uDE00") or built with Character.toChars(codePoint).

Usage notes

  • Unicode escapes are translated first. "\u0022" is not a string containing a quote; it becomes """ before parsing and breaks the literal. Likewise \u000a inside a // comment ends the comment and starts code. Never put \u000a, \u000d or \u0022 in source; use \n, \r and \".
  • \u in comments and strings. A comment like // C:\users\ann is a compile error ("illegal Unicode escape"), because \u starts an escape. A backslash only counts as an escape start if it is preceded by an even number of backslashes: "\\u0041" is the six characters \u0041.
  • Octal is greedy but bounded. "\1234" is \123 followed by 4; "\400" is not valid. Prefer \0 and Unicode escapes over octal in new code.
  • Regex and paths need two backslashes. "\\d" for the regex \d, and "C:\\dir" for a path. In a regex that should match a literal backslash you need four: "\\\\".
  • \n is always one character in Java; System.lineSeparator() or %n give the platform's separator when you write files for humans.
  • Character literals can hold one UTF-16 unit. '\u03a9' is fine; an emoji is not, because it needs two units.
  • Escapes in char arithmetic. '\t' + 1 is the int 10, not a string. Concatenate with a String if you need text.
  • Reading escapes from files. Input data containing the two characters \ and n is not a line feed. Use translateEscapes() (Java 15), a JSON parser, or a custom mapper.