Sunday, December 29, 2024
Google search engine
HomeLanguagesJavaEscape Sequences in Java

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:




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


This code gives a compile time error as :

prog.java:3: error: ')' expected
    System.out.println("Hi geek, welcome to "Lazyroar".");
                                             ^
prog.java:3: error: not a statement
    System.out.println("Hi geek, welcome to "Lazyroar".");
                                                          ^
prog.java:3: error: ';' expected
    System.out.println("Hi geek, welcome to "Lazyroar".");
                                                             ^
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 “Lazyroar” 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:




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


Output: Hi geek, welcome to "Lazyroar".

Some Coding Examples of Java Escape Characters

  1. Java code for the escape sequence \t:




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

    
    

    Output:

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




    // 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! ");
        }
    }

    
    

    Output:(The output depends upon compiler)

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




    // 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?  ");
        }
    }

    
    

    Output:

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




    // 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?  ");
        }
    }

    
    

    Output:(The output depends upon compiler)

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




    // 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?  ");
        }
    }

    
    

    Output:(The output depends upon compiler)

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




    // 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?  ");
        }
    }

    
    

    Output:

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




    // 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?  ");
        }
    }

    
    

    Output:

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




    // 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. ");
        }
    }

    
    

    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.

RELATED ARTICLES

Most Popular

Recent Comments