What is Escape Sequence and How to use it?

In the previous section, we tried our first program using the “println” function to print “Hello world.” Now, let’s try to show “the motto of infolinux is “Just Another your dream”” using the same code we worked in the previous section. Can you do that? You got an error, right? What you need here is an escape sequence function. This article will explain the escape sequence and how to use an escape sequence.

What is Escape Sequence?

Things work differently in Java programming. As we have said, we can not print the sentence we want using the regular sequences. To print special conditions, we must use an escape sequence. We didn’t start with the definition, and it may confuse you.

Let’s solve the first question. It is said to print, “the motto of infolinux is “Just Another your dream””. What can be the code?

Solution: package main;
public class First {
public static void main(String[] args){
System.out.println("the motto of infolinux is \"Just Another your dream\"");
}
}

Here, we used \”……\” to show “Just Another your dream.” So, \” is an escape sequence.

There are eight common types of escape sequences. Please check it out in the table below:

Escape Sequence Description
\” For double quotation in the text
\’ For the single quotation in the text
\n For the new line in the text
\f To insert a formfeed n the text
\r For the carriage return of the selected text
\t To insert a tab space in the text
\\ To insert a backslash in the text
\b To insert a backspace in the text

These are the commonly used escape sequences in Java programming. Except for these, there are other escape sequences, too, such as \s, \ddd, \uxxxx, etc.

How to use it?

Let’s solve another problem so that we can understand the use of the escape sequence easily. Concentrate on the question below.

Problem: Use Java Programming and show the following text.

“What’s the update of coronavirus today?
Nothing much but cured               A             New infected      B.”

Solution:

package main;
public class First {
       public static void main(String[] args){
System.out.println("What’s the update of coronavirus today?\nNothing much but cured\tA\tNew infected\tB ");                
       }
}
What is Escape Sequence and How to use it

Output:

Explanation:

Here we used two different escape sequences. One of them is \n, and the other one is \t. See the coding and judge it with the output. Can you distinguish the difference? Well, \n is used to insert a new line. We used \n after the sentence “What’s the update of coronavirus today?” and the new line started from the sentence “Nothing much but cured      A             new infected                B.”        

Another use of escape sequence we can see here is \t. It means it will provide you with six space distance. Pressing space six times isn’t convenient. And thus we use tab to get that. In programming, we use \t. Here in the problem, we had six spaced distance between A & New infected and New infected & B. Hence, and we used \t in between them in the program.

Like these, the escape sequences are also used in the same way. Depending on your style, we can choose any of them. 

Leave a Reply