Hi! In previous lessons, we've already gotten acquainted with text strings, which are represented by the String class in Java.
Escaping characters - 1
As you probably remember, a string is a sequence of characters. 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.
  • \b - backspace (a step backward in the text or deletion of a single character).
  • \n - new line.
  • \r - carriage return. ()
  • \f - form feed.
  • \' single quote.
  • \" double quote.\\ backslash.
Thus, if the compiler encounters \n 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, \nOf cloudless climes and starry skies\nAnd all that's best of dark and bright\nMeet 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 CodeGym owns the copyright to this lesson:
public class Main {
   public static void main(String[] args) {
       System.out.println("\"Escaping characters\", \u00A9 2018 CodeGym");
   }
}
Console output: "Escaping characters", © 2018 CodeGym 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!
public class Main {
   public static void main(String[] args) {

       System.out.println("\u041c\u0430\u0301\u043e " +

               "\u0426\u0437\u044d\u0434\u0443\u0301\u043d " +

               "\u0028\u043a\u0438\u0442\u002e \u0442\u0440\u0430\u0434\u002e " +

               "\u6bdb\u6fa4\u6771\u002c \u0443\u043f\u0440\u002e " +

               "\u6bdb\u6cfd\u4e1c\u002c \u043f\u0438\u043d\u044c\u0438\u043d\u044c\u003a " +

               "\u004d\u00e1\u006f \u005a\u00e9\u0064\u014d\u006e\u0067\u0029 " +

               "\u2014 \u043a\u0438\u0442\u0430\u0439\u0441\u043a\u0438\u0439 " +

               "\u0433\u043e\u0441\u0443\u0434\u0430\u0440\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 " +

               "\u0438 \u043f\u043e\u043b\u0438\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0439 " +

               "\u0434\u0435\u044f\u0442\u0435\u043b\u044c \u0058\u0058 \u0432\u0435\u043a\u0430\u002c " +

               "\u0433\u043b\u0430\u0432\u043d\u044b\u0439 \u0442\u0435\u043e\u0440\u0435\u0442\u0438\u043a " +

               "\u043c\u0430\u043e\u0438\u0437\u043c\u0430\u002e");
   }
}
Console output: Mao Zedong (Traditional Chinese: 毛澤東, Simplified Chinese: 毛泽东, Pinyin: Máo Zédōng) was a 20th-century Chinese statesman, politician, and the chief theoretician of Maoism. In this example, we used character codes to build a string consisting English and three(!) different types of Chinese characters — traditional, simplified and Latin (Pinyin). And that about sums it up! Now you know enough about escaping characters to use this great tool in your work :)