Read Also: String Interview Questions in Java
What are Escape Sequences in Java
According to Java docs,In java, a character with a backslash(\) just before it is called an escape character or escape sequence.
There are a total of 8 escape characters or escape sequences in java. Please find them below:
Why we need Escape Sequence
In Java, a String represents a sequence of characters. These characters can be alphabets, numerals, punctuation marks, etc.The main point while creating a String in Java is that the entire sequence of characters must be placed in quotation marks. For example:
public class JavaHungry { public static void main(String args[]) { System.out.println(" Alive is awesome!! "); } }
Output:
Alive is awesome!!
But what if the requirement is that String must contain quotation marks i.e quotes within quotes.
For example: " Alive is "awesome"!! "
public class JavaHungry { public static void main(String args[]) { System.out.println(" Alive is "awesome"!! "); } }
Output:
Compilation error
As you might have guessed compiler gets confused while interpreting the interior quotes of the String i.e "awesome"
To make things simple for compiler we need to let it know when a quotation mark is a command (create a String)
or
when it is simply a character(display the word "awesome" along with quotation marks).
To solve the above problem we need character escaping. This is achieved using a special symbol \ (backslash) as shown in red color.
Let's try to print the quotes within quotes example again i.e " Alive is "awesome"!! " using \ backslash.
public class JavaHungry { public static void main(String args[]) { System.out.println(" Alive is \"awesome\"!! "); } }
Output:
Alive is "awesome"!!
What is the Length of the Escape Sequence
An escape sequence is a single character in Java, so its length is 1. For example:public class JavaHungry { public static void main(String args[]) { System.out.println("\n".length()); } }
Output:
1
Escape Octal and Unicode Characters
Octal escapes from \0 to \377 can be shown using escape sequences.Unicode escapes can be shown in the form \uXXXX, where \uXXXX represents \u0000 to \uFFFF
Examples of Escape Sequences in Java
Please find below the examples of escape sequences in Java.public class JavaHungry { public static void main(String args[]) { // Code for escapesequence \t System.out.println("Alive is\t awesome"); // Output: Alive is awesome // Code for escapesequence \b System.out.println("Alive is\b awesome"); // Output: Alive is awesome // Code for escapesequence \n System.out.println("Alive is\n awesome"); /* Output: Alive is awesome */ // Code for escapesequence \r System.out.println("Alive is\r awesome"); /* Output: Alive is awesome */ // Code for escapesequence \f System.out.println("Alive is\f awesome"); // Output: Alive is awesome // Code for escapesequence \' System.out.println("Alive \'is\' awesome"); // Output: Alive 'is' awesome // Code for escapesequence \" System.out.println("Alive \"is\" awesome"); // Output: Alive "is" awesome // Code for escapesequence \\ System.out.println("\\Alive is awesome"); // Output: \Alive is awesome } }
That's all for today, please mention in comments in case you have any questions related to escape sequences in Java with examples.