Escape Sequences in Java

A character with a backslash (\) just before it is an escape sequence or escape character. We use escape characters to perform some specific task. The total number of escape sequences or escape characters in Java is 8. Each escape character is a valid character literal.
The list of Java escape sequences:

Why will we need Escape sequence?
Suppose we would like to run the subsequent java code:

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test {
    public static void main(String[] args)
    {
        System.out.println("Hi geek, welcome to " GeeksforGeeks ".");
    }
}

chevron_right


This code gives a compile time error as :

prog.java:3: error: ')' expected
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                             ^
prog.java:3: error: not a statement
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                                          ^
prog.java:3: error: ';' expected
    System.out.println("Hi geek, welcome to "GeeksforGeeks".");
                                                             ^
3 errors

This happened because the compiler expects nothing but only strings inside the quotation mark but when the compiler found a quotation mark, it expects another quotation mark in the near future (the closing one) and between them, the string of text should be created. During this case, the quotation marks of the word “GeeksforGeeks” gets nested(inside another quotation marks). Once the compiler reaches here, the compiler gets confused. According to the rule, the quotation mark suggests the compiler for creating a string but the compiler was busy with doing that thing previously and the code gives us a compile-time error.

So we should provide proper instructions to the compiler about the quotation mark. i.e, when a quotation is used for creating a string(as a command) and when it is a character itself (the part of the output string).
Similar sorts of confusion arise with other characters also(like- backslashes(), single and double quotation mark (‘, ”)) and these also provide a compile-time error in every case. For Solving these sorts of issues we have to use the java character escaping.



Control Sequence:
A control sequence is nothing however the backslash(\) glued with a character (the character which has to be escaped) is called a control sequence.
Example:
\\ is a control sequence used for displaying a backslash as output.

so let’s use this concept in the previous java code to avoid the compile-time error:

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Test {
    public static void main(String[] args)
    {
        System.out.println("Hi geek, welcome to \"GeeksforGeeks\".");
    }
}

chevron_right


Output: Hi geek, welcome to "GeeksforGeeks".

Some Coding Examples of Java Escape Characters

  1. Java code for the escape sequence \t:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // \t -> It gives a tab between two words.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning\t Geeks! ");
        }
    }

    chevron_right

    
    

    Output:

    Good Morning     Geeks!  
  2. Java code for the escape sequence \b :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // The escape sequence \b is a backspace character
    // It moves the cursor one character back with
    // or without deleting the character(depending upon compiler)
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning\bg Geeks! ");
        }
    }

    chevron_right

    
    

    Output:(The output depends upon compiler)

    Good Morning Geeks! 
     
  3. Java code for the escape sequence \n :
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \n escape sequence is for a new line.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \n How are you all?  ");
        }
    }

    chevron_right

    
    

    Output:

    Good Morning Geeks! 
    How are you all? 
    
  4. Java code for the escape sequence \r:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \r escape sequence is a carriage return character 
    // It moves the output point back to the beginning of the line without moving down a line (usually).
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \r How are you all?  ");
        }
    }

    chevron_right

    
    

    Output:(The output depends upon compiler)

      Good Morning Geeks! 
      How are you all? 
    
  5. Java code for the escape sequence \f:
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \f escape sequence is a form feed character
    // It is an old technique and used to indicate a page break.
      
    public class Test {
        public static void main(String[] args)
        {
            System.out.println("Good Morning Geeks! \f How are you all?  ");
        }
    }

    chevron_right

    
    

    Output:(The output depends upon compiler)

    Good Morning Geeks!  
                         How are you all? 
     
  6. Java code for the escape sequence \’
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \' escape sequence is for printing a single quotation mark on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("Good Morning \'Geeks!\' How are you all?  ");
        }
    }

    chevron_right

    
    

    Output:

    Good Morning 'Geeks!' How are you all? 
  7. Java code for the escape sequence \”
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \" escape sequence is for printing a double quotation mark on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("Good Morning \"Geeks!\" How are you all?  ");
        }
    }

    chevron_right

    
    

    Output:

    Good Morning "Geeks!" How are you all? 
  8. Java code for the escape sequence \\
    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // This \\ escape sequence is for printing a backslash on the text string
      
    public class Gfg {
        public static void main(String[] args)
        {
            System.out.println("\\- this is a backslash. ");
        }
    }

    chevron_right

    
    

    Output:

    \- this is a backslash. 

    Explanation:It contains two backslashes, this means after reading the first \ the compiler read the next \ as a new character.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.