Hi! In previous lessons, we've already gotten acquainted with text strings, which are represented by the String class in Java.
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.
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 2019 CodeGym");
}
}
Console output:"Escaping characters", © 2019 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("\u004d\u0061\u006f \u005a\u0065\u0064\u006f\u006e\u0067 " +
"\u0028\u0054\u0072\u0061\u0064\u0069\u0074\u0069\u006f\u006e\u0061\u006c " +
"\u0043\u0068\u0069\u006e\u0065\u0073\u0065\u003a \u6bdb\u6fa4\u6771\u002c " +
"\u0053\u0069\u006d\u0070\u006c\u0069\u0066\u0069\u0065\u0064 " +
"\u0043\u0068\u0069\u006e\u0065\u0073\u0065\u003a \u6bdb\u6cfd\u4e1c\u002c " +
"\u0050\u0069\u006e\u0079\u0069\u006e\u003a \u004d\u00e1\u006f " +
"\u005a\u00e9\u0064\u014d\u006e\u0067\u0029 \u0077\u0061\u0073 \u0061 " +
"\u0032\u0030\u0074\u0068\u002d\u0063\u0065\u006e\u0074\u0075\u0072\u0079 " +
"\u0043\u0068\u0069\u006e\u0065\u0073\u0065 " +
"\u0073\u0074\u0061\u0074\u0065\u0073\u006d\u0061\u006e\u002c " +
"\u0070\u006f\u006c\u0069\u0074\u0069\u0063\u0069\u0061\u006e\u002c " +
"\u0061\u006e\u0064 \u0074\u0068\u0065 \u0063\u0068\u0069\u0065\u0066 " +
"\u0074\u0068\u0065\u006f\u0072\u0065\u0074\u0069\u0063\u0069\u0061\u006e " +
"\u006f\u0066 \u004d\u0061\u006f\u0069\u0073\u006d\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 :)
To reinforce what you learned, we suggest you watch a video lesson from our Java CourseMore reading: |
---|
GO TO FULL VERSION