8 different Escape Sequence in Java with Examples


JAVA

Reviewer: Deepak Prasad

Introduction to escape sequence in java

An escape sequence in java refers to a character proceeded by a backslash(\), the escape sequences have special meanings. In java, while declaring a string, a certain set of rules are applied to get desired resultant string e.g. if I want to print quotation marks around a string I cannot just put quotation marks around it, the code below will explain this :

import java.util.*;
class main_class {
    public static void main(String[] args)
    {
        System.out.println(""congratulations!!!!!!!!!"");
    }
}

This code results in an error that says :

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
The left-hand side of an assignment must be a variable
Syntax error on token "congratulations", invalid AssignmentOperator
at main.main_class.main(main_class.java:8)

For these purposes, we use escape sequences in java. If we put a backslash at both sides of this string it will display the desired output.

import java.util.*;
class main_class {
    public static void main(String[] args) {
        //use an escape sequence in java to show quotation marks 
        System.out.println("\"congratulations!!!!!!!!!\"");
    }
}

This code will output :

"congratulations!!!!!!!!!"

To study more about java strings you can check Java String Concatenation Examples [4 Methods]

 

Different Escape Sequences used in Java

There are total of 8 escape sequences in Java each having a unique function.

Escape Sequence Function
\t Add a tab space
\r sends cursor to next line
\n next line
\f page break
\b backspace
\' add single quotation marks around string
\" add double quotation marks around string
\\ add \ where needed

 

1. The  \t escape sequence:

This escape sequence in java gives a tab space where it is placed. Sample code given below shows how it works :

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the \t escape sequence in java
        System.out.println("escape\tsequence\tin\tjava");
    }
}

The output of this code is :

escape sequence in java

 

2. The \b  escape sequence :

This is the backspace escape sequence in java, it moves the cursor one character back, it either deletes the character or it does not, varying compiler to compiler, the code below will show how this works :

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the /b escape sequence in java
        System.out.println("escape\b sequence\b in\b java");
    }
}

The output of this code is :

escap sequenc i java         (in some compilers this output may vary)

 

3. The  \n escape sequence : 

This escape sequence takes the cursor to the new line, it adds the next line from the point where it is placed. The code below demonstrates how :

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the /n escape sequence 
        System.out.println("escape sequence \nlinux cloud tutorial \njava programming tutorials");
    }
}

The output of this code is :

escape sequence 

linux cloud tutorial 

java programming tutorials

 

4. The \r escape sequence 

This is the carriage return function, in-display it works exactly like the \n sequence but in theory, it moves the point of output to the start of the line, thus showing it on the next line

The code below demonstrates how this works :

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the /r escape sequence in java
        System.out.println("escape sequence\rlinux cloud tutorial \rjava programming tutorials");
    }
}

The output of this code is :

escape sequence

linux cloud tutorial 

java programming tutorials

 

5. The /f escape sequence in java

This escape sequence works as a form feed character, this is used for page breaking. The code below displays how:

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the /f escape sequence 
        System.out.println("escape sequence in java \flinux cloud tutorial \fjava programming tutorials");
    }
}

The output of the following code is

escape sequence in java 

                                         linux cloud tutorial  

                                                                       java programming tutorials

 

6. The  \’ Escape sequence in java

This escape sequence is used when a string needs single quotations around it. In java, this can not be done by simply putting quotation marks around the string, a backslash before the quotation is placed. The code below demonstrates how this works.

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the \' escape sequence 
        System.out.println("\'escape sequence\'");
    }
}

The output of this code is :

'escape sequence '

 

7. The \" Escape sequence in java

This escape sequence is used when a string needs double quotations around it. In java, this can not be done by simply putting double quotation marks around the string, a backslash before the double quotation is placed. The code below demonstrates how this works.

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the \" escape sequence 
        System.out.println("\"escape sequence\"");
    }
}

The output of this code is :

"escape sequence"

8. The \\ escape sequence in java

This escape sequence is used to place a backslash in the string, one backslash will always result in an error because after a backslash the compiler expects an escape sequence character, so if there is no escape sequence character in java after a backslash, another one is needed to have one in the string. The code below will demonstrate how this works

import java.util.*;
class main_class {
    public static void main(String[] args) {
        // the \\ escape sequence in java
        System.out.println("\\escape sequence\\");
    }
}

The output of this code is :

\escape sequence\

 

Practice code for better understanding of escape sequence in Java:

Since now all the escape sequences are discussed, please take a look at the code below and determine its output. This code is for practice so dry run the code before running it on a compiler.

import java.util.*;
class main_class {
    public static void main(String[] args)
    {
        System.out.println("\'escape sequence\'");
        System.out.println("\"escape sequence \"");
        System.out.println("\nescape sequence\n in java\\");
        System.out.println("\nescape sequence \r in java\\");
        System.out.println("\bescape sequence ");
        System.out.println("escape ");

    }
}

 

Conclusion

An escape sequence refers to a character proceeded by a backslash(\), the escape sequences have special meanings. For these purposes, we use escape sequences. In this article, we have studied all the escape sequences of java i.e /n /t /r /b /’ // /” and  /f.

 

Further Reading

For further reading about java escape sequences, see these articles
Escape Sequences
Strings

 

Azka Iftikhar

Azka Iftikhar

She is proficient in multiple programming languages, including C++, GO, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can check her professional profile on GitHub which captures her experience and portfolio.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment