Escape Sequences Tutorial

There are certain operations in Java that create problems when text is displayed to the console or elsewhere. Things like a tab, carriage return, or double quote needed to be included in string literals. Java provides us the ability to handle situations like these with special syntax called escape sequences. When a character follows a backslash (\) in either a character literal or a string literal, it is called an escape sequence. An escape sequence tells the compiler to perform a special instruction at the point of the escape sequence. An escape sequence is often referred to as an escape code or escape character.

  • char b = '\n'; // a new line escape sequence
  • String a = "This is how you surround a letter, word, or phrase with \"double quotes\" in a string literal.";
The following is a chart of all the escape sequences Java supports:
    Escape Sequence         Description
    \t 	                    Horizontal Tab
    \b 	                    Backspace
    \n 	                    New Line
    \r                      Carriage Return
    \f 	                    Formfeed
    \' 	                    Single Quote Character
    \" 	                    Double Quote Character
    \\ 	                    Backslash Character
    \xxx                    Octal number (xxx is the Octal number)
    \uxxxx                  Hexadecimal number (xxxx is the Hexadecimal number)
    



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md EscapeSequences
C:\Java>cd EscapeSequences
C:\Java\EscapeSequences>Notepad EscapeSequences.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class EscapeSequences {
    public static void main(String args[]) {
        String a = "Using the \"Tab\" escape sequence to align columns."; // example of the double quote \" escape sequence
        String b = "Odd\tEven\n1\t2\n3\t4\n5\t6\n7\t8"; // example of the \t tab and the \n new line
        String c = "Let\'s create more escape sequence examples."; // example of the \' single quote
        String d = "Your cursor should be at C:\\Java\\EscapeSequences>"; // example of the \\ backslash
        String e = "ASCII A using an Octal escape sequence = \101"; // note the lack of a leading 0
        String f = "ASCII A using an Hexadecimal escape sequence = \u0041"; 
        String typo = "I can\'t spelll\b at the moment."; // \' single quote and \b backspace	

        System.out.println(a);
        System.out.println(b);
        System.out.println(c);
        System.out.println(d);
        System.out.println(e);
        System.out.println(f);
        System.out.println(typo);
    }
}

Now switch back to the command prompt (CMD) and type in javac EscapeSequences.java and press Enter.
Now type in java EscapeSequences and press Enter.


C:\Java\EscapeSequences>javac EscapeSequences.java
C:\Java\EscapeSequences>java EscapeSequences
Using the "Tab" escape sequence to align columns.
Odd     Even
1       2
3       4
5       6
7       8
Let's create more escape sequence examples.
Your cursor should be at C:\Java\EscapeSequences>
ASCII A using an Octal escape sequence = A
ASCII A using an Hexadecimal escape sequence = A
I can't spell at the moment.


Final thoughts

I use escape sequences quite often; reading and writing to files, changing directories, etc. One thing to remember when you are creating a string literal is that you cannot type in a single backslash, it will cause a strange compiler error because the backslash character flags the compiler that a special escape sequence is going to follow.


Tutorials