Escape Sequences In Java

Escape Sequences In Java

First of all, let’s talk about a situation in which you need to have single quotes in your string, what are you going to do in that case? It’s easy! There’s no problem in writing single quotes into the double quotes. The problem arises when we try writing double quotes inside the double quotes. Just imagine that we need to print something like this as an output – I am learning “Java” from “GyaniPandit”.

Well, it is a little bit tricky, but we are going to talk about it a little bit later. Here is the program in which we test the single quotes inside the double quotes. Well, this is pretty easy and we do not have to do anything different in this. Have a look-

public class Main {
public static void main(String[] args) {
System.out.println(“I am learning java from GyaniPandit’s Java course.”);
}
}

If we try running this program, we can comfortably have the output as we have desired. The issue arises when we try putting double quotes into double quotes, instead of the single quote, confusing the compiler. If you observe carefully, the two double quotes of the string literal are doing the work of showing starting and ending of a string.

Now, if we try to use them inside our string highlighting something else, it will create a problem and the code won’t compile. Let’s have a look at this thing too –

public class Main {
public static void main(String[] args) {
System.out.println(“I am learning “java” from “GyaniPandit””);
}
}

The above code looks totally fine if we look at it normally. But if you were a compiler, you would have said that – Hey buddy, you can’t just do that…!

But when we try executing the code, it is going to give an error, or if you are using some IDE, you know about the errors right even before executing the program. We may think that – Ok… the string starts with the first double quote, then it says I am learning and then there comes java in double-quotes, then from, and then again GyaniPandit in double-quotes. But life is not so easy!

The double quotes were already doing something else, marking the starting and ending of a String, and we tried to interrupt that. So, in short, we got an error. We wanted that double quotes to be a part of our String, but we ended up having an error.

But don’t worry, if there is a problem, there has to be a solution. No… I am not saying that don’t use double quotes inside our strings. Avoiding a problem does not necessarily solve it right? What we need to do here is to use an escape sequence. This is like we are telling that – hey… we want you to interpret this thing after the backslash sign in a different way.

In java, some character after the backslash (\) is an escape sequence. It is also called an escape character. These escape sequences are meant to be inside the quotation marks. These different escape characters are understood differently as a single character and have a special meaning.

So, in short, I just wanted to tell you that we can make use of the escape sequence here. Have a look at this code now, after adding the escape sequence.

public class Main {
public static void main(String[] args) {
System.out.println(“I am learning \”java\” from \”GyaniPandit\””);
}
}

So, if you now try executing the program, you will find that the program runs happily without any errors, and the output is also as expected. This is kind of easy. Now, the double-quote after the backslash is actually a double quote that we can use inside the string literal. There are some more escape sequences that you may have used earlier in another programming language like C, or you (we) may need to use them somewhere in the future.

Please refer to the below table which highlights some more escape characters, and also what they do.

Escape CharacterWhat do they mean?
\nThis is used to add a new line in the text
\t  This is used to add a tab in the text
\\This is used to insert a backslash in the text
\bThis is used to insert a backspace in the text

You can consider exploring more about the escape character since they are useful in many situations. However, often we may want to use these above-mentioned ones. Again, if we need to use something new, that will be mentioned and explained in a way that we can understand and move ahead comfortably.