HOWTO · Java

How to Escape Characters in Java

This article explains the common escape sequences in Java

On this page

Java escape sequences let you include control characters and quote marks in string and character literals. The eight entries below are common examples, not an exhaustive catalog of every escape form Java supports.

Java Escape Sequences at a Glance

Sequence Meaning
\t Horizontal tab
\b Backspace control character
\n Line feed, commonly used as a line break
\r Carriage return control character
\f Form feed control character
\' Single quote character, when needed in a literal
\" Double quote character, when needed in a literal
\\ A literal backslash

An escape sequence begins with a backslash in Java source. The compiler interprets it as part of the literal, so the value contains the represented character rather than the two source characters used to spell the escape. For example, \n in a string represents a line-feed character.

Use Escape Sequences in String and Character Literals

Java string literals use double quotation marks, as in "text"; character literals use single quotation marks and contain one char, as in 'A'. Escape a quote when it matches the delimiter: write \" inside a string and \' inside a character literal. The other quote can appear without escaping: a string can contain ', and a character literal can contain ". A backslash in the value is written as \\ in the source.

A literal such as "first\nsecond" contains two lines when its value is interpreted, while "column A\tcolumn B" places a tab between the labels. These escapes are useful when a value must include formatting or punctuation that would otherwise end the literal. A raw line break cannot be inserted into an ordinary quoted string or character literal; use \n in a string when a line-feed character is wanted.

What Each Escape Sequence Does

The tab sequence inserts a horizontal tab. Its displayed width depends on the application showing the text, since tabs can align to different tab stops. A line feed adds the line-feed control character, which many terminals and text viewers display as a new line.

Backspace represents a control character; it does not erase earlier source text or guarantee that a displayed character disappears. Carriage return moves to the start of a line in destinations that interpret it that way. Form feed is another control character, often associated with page breaks, but many modern text displays show no special page effect. These characters remain part of the value even when a destination does not visibly render them.

The quote escapes represent quote characters, and \\ represents one backslash. Use \' when a single quote would otherwise close a character literal, and \" when a double quote would otherwise close a string. In a string, single quotes need no special treatment; in a character literal, a double quote can be written directly between the single-quote delimiters.

Common Pitfalls and Output Behavior

Here is a complete Java program that combines a tab, a line feed, a single-quote character, and backslashes in string and character values. The supplied run produced this output:

Run this example:

public class EscapeCharacters {
    public static void main(String[] args) {
        String message = "Name:\tAda\nLanguage:\tJava";
        char quote = '\'';
        String path = "C:\\Java\\bin";

        System.out.println(message);
        System.out.println("Quote: " + quote);
        System.out.println("Path: " + path);
    }
}
Name:	Ada
Language:	Java
Quote: '
Path: C:\Java\bin

When troubleshooting, separate the value created by Java from the way a terminal, log viewer, or editor displays it. A line feed is not the same character as a carriage return, and a carriage return does not inherently add a new line. Depending on the destination, control characters can move the cursor, affect formatting, or appear to have no visible effect. Avoid describing those display effects as differences in compiler behavior: the escape spelling determines the character in the value, while the output destination determines how that character is presented.