Escape sequences on Java string literals
- Last Updated: October 24, 2025
- 1 minute read
- Corticon
- Documentation
A character preceded by a backslash (\) is an escape sequence and has special meaning to the compiler. The following table shows the Java escape sequences:
| Escape Sequence | Description |
| \t | Insert a tab in the text at this point. |
| \b | Insert a backspace in the text at this point. |
| \n | Insert a newline in the text at this point. |
| \r | Insert a carriage return in the text at this point. |
| \f | Insert a form feed in the text at this point. |
| \' | Insert a single quote character in the text at this point. |
| \" | Insert a double quote character in the text at this point. |
| \\ | Insert a backslash character in the text at this point. |
When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes.
Java specifics:
- Character Literals: Java has distinct character literals (single quotes) which also utilize escape sequences for special characters.
- Octal and Hexadecimal Escapes: Java supports octal escapes (e.g.,
\123) and hexadecimal escapes (such as,\xAB) for representing characters by their numeric values.