CodeGym/Java Blog/Strings in Java/Java Escape Characters
Author
Vasyl Malik
Senior Java Developer at CodeGym

Java Escape Characters

Published in the Strings in Java group
members
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.
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 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 Course
Comments (39)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Lunita
Level 4 , Dominican Republic
24 January 2023, 22:58
Java Escape Characters In Java, if a character is preceded by a backslash (\) is known as Java escape sequence or escape characters. It may include letters, numerals, punctuations, etc. Remember that escape characters must be enclosed in quotation marks (" "). The Java compiler interprets these characters as a single character that adds a specific meaning to the compiler. These are the valid character literals.
\'	'	Single quote
\"	"	Double quote
\\	\	Backslash
Also:
\n	New Line
\r	Carriage Return
\t	Tab
\b	Backspace
\f	Form Feed
Anonymous #10783379
Level 7 , China
17 August 2021, 15:57
可以,很有用
Artur
Level 7 , Poland
26 July 2021, 09:52
\f - what does it do? My program makes me little squares when i use \f
sachin
Level 7 , Singapore, Singapore
Expert
18 June 2021, 06:29
thanks for the detailed explanation.
Javatrix6916
Level 7 , Krakow, Poland
16 November 2020, 10:35
Trzeba przyznać, że jest to bardziej zrozumiałe niż na kursie. Jestem strasznym nieogarem, ale wszystko rozumiem :) Dziękuję, twórcy!
Nina Tuttle
Level 4 , Champaign, United States
20 October 2020, 13:34
very nice explanation
Noah Jones
Level 4 , Leesburg, United States
29 September 2020, 01:06
Does anyone know how to bookmark a page? I can't seem to find the button.
25 October 2020, 12:30
Ctrl + D in Chrome and Firefox.
Noah Jones
Level 4 , Leesburg, United States
5 November 2020, 22:28
Sorry, thought there was a way to make an internal bookmark on the site. Guess they were talking about just making a browser bookmark😬
Michael Scanlan
Level 4 , Kalispell, United States
25 January 2021, 22:56
There is, if you click the like button at the bottom of the article a menu with the bookmark option will pop up
claudia
Level 19 , Germany, Germany
13 May 2022, 10:52
Thanks, Michael.
claudia
Level 19 , Germany, Germany
13 May 2022, 10:53
Thanks, Hidden.
Andrei
Level 41
25 August 2020, 08:53
Very nice article, well formulated! Thank you.
Neurobot
Level 10 , Göttingen, Germany
3 June 2020, 19:11
verry usefull this article
太古天霸 Web Java Developer
4 May 2020, 07:39
I like this!