Escape Characters in Java
By: Kajal Rawal
Tags: Escapesequenceforspace, Character, String, Java

Learn how we can use escape sequence in Java

These characters can be any letters, numerals, punctuation marks and so on. The main thing when creating a string is that the entire sequence must be enclosed in quotation marks:

public class Main {

public static void main(String[] args) {

String alex = new String ("My name is Alex. I'm 20!");

}

}

But what do we do if we need to create a string that itself must contain quotation marks? For example, suppose we want to tell the world about your favorite book:

public class Main {

public static void main(String[] args) {

String myFavoriteBook = new String ("My favorite book is "Twilight" by Stephanie Meyer");

}

}

It seems the compiler is unhappy about something! What do you think the problem could be? And what does it have to do with quotation marks? In fact, it’s all very simple. The compiler interprets quotation marks in a very specific way, i.e. it expects strings to be wrapped in them. And every time the compiler sees “, it expects that the quotation mark will be followed by a second quotation mark, and that the content between them is the text of a string to be created by the compiler. In our case, the quotation marks around the word “Twilight” are inside other quotation marks. When the compiler reaches this piece of text, it simply doesn’t understand what it is expected to do. The quotation mark suggests that a string must be created. But that’s what the compiler is already doing! Here’s why: simply speaking, the compiler gets confused about what it is expected to do. “Another quotation mark? Is this some kind of mistake? I’m already creating a string! Or should I create another one? Argh!…:/” We need to let the compiler know when a quotation mark is a command (“create a string!”) and when it is simply a character (“display the word “Twilight” along with quotation marks!”). To do this, Java uses character escaping. This is accomplished using a special symbol: \ . This symbol is normally called "backslash". In Java, a backslash combined with a character to be "escaped" is called a control sequence. For example, \" is a control sequence for displaying quotation marks on the screen. Upon encountering this construct in your code, the compiler will understand that this is just a quotation mark that should be displayed on the screen. Let's try changing our code with the book:

public static void main(String[] args) {

String myFavoriteBook = new String ("My favorite book is \"Twilight\" by Stephanie Meyer");

System.out.println(myFavoriteBook);

}

}

We’ve used \ to escape our two "internal" quotation marks. Let's try running the main() method... Console output: My favorite book is "Twilight" by Stephanie Meyer Excellent! The code worked exactly how we wanted it to! Quotation marks are by no means the only characters we may need to escape. Suppose we want to tell someone about our work:

public class Main {

public static void main(String[] args) {

String workFiles= new String ("My work files are in D:\Work Projects\java");

System.out.println(workFiles);

}

}

Another error! Can you guess why? Once again, the compiler doesn’t understand what to do. After all, the compiler doesn’t know \ as anything other than a control sequence! It expects the backslash to be followed by a certain character that it must somehow interpret in a special way (such as a quotation mark). But, in this case, \ is followed by ordinary letters. So the compiler is confused again. What should we do? Exactly the same thing as before: we just add another \ to our \ !

public class Main { public static void main(String[] args) { String workFiles= new String ("My work files are in D:\\Work Projects\\java");

System.out.println(workFiles); }

}

Let’s see what we get: Console output: My work files are in D:\Work Projects\java Super! The compiler immediately determines that the \ are ordinary characters that should be displayed along with the rest. Java has quite a lot of control sequences. Here's the full list:

\t - tab.

- tab. \b - backspace (a step backward in the text or deletion of a single character).

- backspace (a step backward in the text or deletion of a single character).

- new line.

- new line. \r - carriage return. ()

- carriage return. () \f - form feed.

- form feed. \' single quote.

single quote. \" double quote.

double quote. \\ backslash.

Thus, if the compiler encounters

in the text, it understands that this is not just a symbol and a letter to display on the console, but rather a special command to "move to a new line!". For example, this may be useful if we want to display part of a poem:

public class Main {

public static void main(String[] args) {

String byron = new String ("She walks in beauty, like the night,

Of cloudless climes and starry skies

And all that's best of dark and bright

Meet in her aspect and her eyes...");

System.out.println(byron);

}

}

Here’s what we get: Console output: She walks in beauty, like the night, Of cloudless climes and starry skies And all that’s best of dark and bright Meet in her aspect and her eyes… Just what we wanted! The compiler recognized the escape sequence and output an excerpt of the poem on 4 lines.

Escape Unicode characters

Another important topic that you need to know about in connection with escape characters is Unicode. Unicode is a standard character encoding that includes the symbols of almost every written language in the world. In other words, it’s a list of special codes that represent nearly every character in any language! Naturally, this is a very long list and nobody learns it by heart :) If you want to know where it came from and why it became necessary, read this informative article: https://docs.oracle.com/javase/tutorial/i18n/text/unicode.html All Unicode character codes have the form “ u +<hexadecimal digit>". For example, the well-known copyright symbol is represented by u00A9. So, if you need to use this character when working with text in Java, you can escape it in your text! For example, we want to inform everyone that Kajal Rawal owns the copyright to this lesson:

public class Main {

public static void main(String[] args) {

System.out.println("\"Escaping characters\", \u00A9 2020 KajalRawal");

}

}

Console output: “Escaping characters”, © 2020 KajalRawal Great, it all worked out! But it’s not just about special symbols! You can use Unicode and escape characters to encode text written simultaneously in different languages. And even text written in several different dialects of the same language!