Escape Sequences in Java with Examples

Escape characters (also called escape sequences or escape codes) in general are used to signal an alternative interpretation of a series of characters. In Java, a character preceded by a backslash (\) is an escape sequence and has special meaning to the java compiler. 

When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence: She said "Hello!" to me. you should write:

System.out.println("She said \"Hello!\" to me.");


Escape sequences available in java are:

\t - Insert a tab in the text at this point.
\b - Insert a backspace in the text at this point.
\n - Insert a newline in the text at this point.
\r - Insert a carriage return in the text at this point.
\f - Insert a formfeed in the text at this point.
\' - Insert a single quote character in the text at this point.
\" - Insert a double quote character in the text at this point.
\\ - Insert a backslash character in the text at this point.

An escape sequence is a single character. For example 

System.out.println("\n".length()); will print 1.



Question 1: What will System.out.println("\".length()); give?

Question 2: What will System.out.println("a\".length() + \"b".length()); print?

Question 3: If \u0022 is the Unicode escape for double quote ("), then what will the below line print:


Quiz Answers

Answer 1

It will give a compilation error because \" is taken as an escape sequence character and hence the string literal needs another double quotes to be properly closed.

Answer 2

Answer is 16. Here \" that comes before .length and b are escape sequences. Hence the string literal whose length we are finding out is a\".length() + \"b . Also  \" is a single character. Therefore length of a\".length() + \"b is 16.

Answer 3

System.out.println("a\u0022.length() + \u0022b".length());

It will print 2. Java provides no special treatment for Unicode escapes within string literals.

Explanation: The compiler translates Unicode escapes into the characters they represent before it parses the program into tokens, such as strings literals. You can use escape sequences in character literals as well as in string literals. You can actually put any ASCII character into a string literal or a character literal by using a special kind of escape sequence called an octal escape, but it is preferable to use normal escape sequences where possible. Both normal escape sequences and octal escapes are far preferable to Unicode escapes because unlike Unicode escapes, normal escape sequences and octal escapes are processed after the program is parsed into tokens.

Quick Notes Finder Tags

Activities (1) advanced java (1) agile (3) App Servers (6) archived notes (2) ArrayLists (1) Arrays (2) Best Practices (12) Best Practices (Design) (3) Best Practices (Java) (7) Best Practices (Java EE) (1) BigData (3) Chars & Encodings (6) coding problems (2) Collections (15) contests (3) Core Java (All) (53) course plan (2) Database (12) Design patterns (8) dev tools (3) downloads (2) eclipse (9) Essentials (1) examples (14) Exception (1) Exceptions (4) Exercise (1) exercises (6) Getting Started (18) Groovy (2) hadoop (4) hibernate (77) hibernate interview questions (6) History (1) Hot book (5) http monitoring (2) Inheritance (4) intellij (1) java 8 notes (4) Java 9 (1) Java Concepts (7) Java Core (8) java ee exercises (1) java ee interview questions (2) Java Elements (16) Java Environment (1) Java Features (4) java interview points (4) java interview questions (4) javajee initiatives (1) javajee thoughts (3) Java Performance (6) Java Programmer 1 (12) Java Programmer 2 (8) Javascript Frameworks (1) Java SE Professional (1) JPA 1 - Module (6) JPA 1 - Modules (1) JSP (1) Legacy Java (1) linked list (3) maven (1) Multithreading (16) NFR (1) No SQL (1) Object Oriented (9) OCPJP (4) OCPWCD (1) OOAD (3) Operators (4) Overloading (2) Overriding (2) Overviews (1) policies (1) programming (1) Quartz Scheduler (1) Quizzes (17) RabbitMQ (1) references (2) resources (1) restful web service (3) Searching (1) security (10) Servlets (8) Servlets and JSP (31) Site Usage Guidelines (1) Sorting (1) source code management (1) spring (4) spring boot (3) Spring Examples (1) Spring Features (1) spring jpa (1) Stack (1) Streams & IO (3) Strings (11) SW Developer Tools (2) testing (1) troubleshooting (1) user interface (1) vxml (8) web services (1) Web Technologies (1) Web Technology Books (1) youtube (1)