Last Updated on March 31, 2023 by Prepbytes
In any programming language, we have some non-printable characters that we want to print and every language has its own way of dealing with this issue. Escape sequence in java is the way to deal with this. Escape sequence in java can be referred to as special characters that are used to represent the characters that are difficult to type in the program. We can use them to manipulate the strings and control the output of the program. While moving further in this article we will learn all about escape sequence in java followed by examples of each one.
Escape Sequence in Java
An escape sequence in java is a combination of specific characters or a sequence of characters with a backslash. Whenever the escape sequence is encountered in a string literal then it is replaced with a special character or sometimes with a sequence of characters. We use them to represent the special characters that either cannot be directly typed into the string literally or have special meaning.
There are a total of 8 escape sequence in java.
- \t: It is used to insert a tab in the text.
- \b: It is used to insert a backspace in the text.
- \’: It is used to insert a single quote of character in the text.
- \f: It is used to insert a form feed in the text.
- \”: It is used to insert a double-quote character in the text.
- \n: It is used to insert a new line in the text.
- \r: It is used to insert a carriage return in the text.
- \: It is used to insert a backslash character.
What is the need to use Escape Sequence in Java?
Suppose you want to print:
class Example { public static void main(String[] args) { System.out.println("Hi Coders, welcome to " Prepbytes "."); } }
Output
Main.java:4: error: ')' expected
System.out.println("Hi Coders, welcome to " Prepbytes ".");
^
Main.java:4: error: not a statement
System.out.println("Hi Coders, welcome to " Prepbytes ".");
^
Main.java:4: error: ';' expected
System.out.println("Hi Coders, welcome to " Prepbytes ".");
^
Explanation
This occurred because the compiler, which normally only anticipates strings inside of quotation marks, discovered a quotation mark before anticipating the closing quotation mark, which is where the string of text was supposed to be created. In this instance, the phrase "Prepbytes"’ quotation marks are nested. (inside another quotation mark). When the compiler arrives at this point, it becomes perplexed. The code generates a compile-time error because the quotation mark instructs the computer to create a string even though the compiler was already engaged in another task.
To prevent from this we should provide proper instructions to the compiler about the quotation mark. A similar type of problem may occur in cases of backslashes and double quotation marks. So we use escape sequence in java to avoid these types of cases and situations.
Control Sequence
We can use control sequences to solve the above problem they are nothing but backslash with a character.
Code
class Example { public static void main(String[] args) { System.out.println("Hi Coders, welcome to \" Prepbytes\"."); } }
Output
Hi Coders, welcome to " Prepbytes".
Examples of Escape Sequence in Java
Now, we will discuss the example of each of the escape sequence in java.
Example of Escape Sequence in Java: \t – Tab
The \t escape sequence represents a tab character. It is used to insert a horizontal tab into a string literal. The tab character is used to align text in a program.
Code Implementation
class Example { public static void main(String[] args) { System.out.println("Good Evening\t Coders! "); } }
Output
Good Evening Coders!
Explanation of the above example
Because of \t we get a tab space between the characters in the above code.
Example of Escape Sequence in Java: \b – Backspace
The \b escape sequence represents a backspace character. It is used to move the cursor one position back in a string. This is useful for creating a typewriter effect in a program.
Code Implementation
class Example { public static void main(String[] args) { System.out.println("Good Evening\bgCoders! "); } }
Output
Good Evening Coders!
Explanation
The output totally depends on the compiler but it moves the position one bit before.
Example of Escape Sequence in Java: \n – Newline
The \n escape sequence represents a newline character. It is used to insert a newline into a string literal. The newline character is used to break a line of text into multiple lines.
Code Implementation
class Example { public static void main(String[] args) { System.out.println("Good Evening\nCoders! "); } }
Output
Good Evening
Coders!
Explanation
By using the /n escape character the rest of the string moves to the new line.
Example of Escape Sequence in Java: \r – Carriage Return
The \r escape sequence represents a carriage return character. It is used to move the cursor to the beginning of the current line. This is useful for creating a typewriter effect in a program.
Code Implementation
class Example { public static void main(String[] args) { System.out.println("Good Evening\rCoders! "); } }
Output
Good Evening
Coders!
Explanation
It moves the cursor back to the beginning of the current line.
Example of Escape Sequence in Java: \f – Form Feed
The \f escape sequence represents a form feed character. It is used to insert a form feed into a string literal. The form feed character is used to start a new page in a printed document.
Code Implementation
class Example { public static void main(String[] args) { System.out.println("Good Evening \f Coders! "); } }
Output
Good Evening Coders!
Explanation
The output will totally depend on the compiler. It will show the page break and the start of a new document.
Example of Escape Sequence in Java: \’ – Single Quote
The ‘ escape sequence represents a single quote character. It is used to insert a single quote into a string literal. The single quote is used to enclose a character literal.
Code Implementation
class Prepbytes { public static void main(String[] args) { System.out.println("Good Morning \'Coders!\' How are you all? "); } }
Output
Good Morning 'Coders!' How are you all?
Explanation of the Above Example
We have inserted a single quote in the string.
Example of Escape Sequence in Java: \" – Double Quote
The " escape sequence represents a double quote character. It is used to insert a double quote into a string literal. The double quote is used to enclose a string literal.
Code Implementation
class Prepbytes { public static void main(String[] args) { System.out.println("Good Morning \"Coders!\" How are you all? "); } }
Output
Good Morning "Coders!" How are you all?
Explanation
In this example we have added a double quote in the string.
Example of Escape Sequence in Java:\ backslash character
This is used for printing the backslash on the text in the output.
Code Implementation
class Prepbytes { public static void main(String[] args) { System.out.println("\\- this is a backslash. "); } }
Output
\- this is a backslash.
Explanation
In the above example we have printed the backslash using the escape character.
Applications of Escape Sequence in Java
The escape sequence in java are widely applicable in various domains and problems some of the applications are mentioned below:
- They are used to insert special characters in strings like double and single quotes.
- We can use the escape sequence in java to insert control characters like tab, carriage return, etc.
- We can represent the Unicode characters using the escape sequence in java.
Conclusion
Escape sequences in Java are a set of characters that have a special meaning when they appear in a string literal. They allow you to represent characters that would be difficult or impossible to include directly in a string. An escape sequence is made up of a backslash () followed by a character or a combination of characters.
Escape sequences are useful for creating strings that contain special characters, such as quotation marks or backslashes. Escape sequences in Java are a powerful tool for representing special characters within string literals. They allow you to create more complex strings and can help you avoid syntax errors that might otherwise occur when trying to include certain characters directly in a string.
Frequently Asked Questions
Here are some of the frequently asked questions about escape sequence in java.
1. What happens if I use an invalid escape sequence?
If you use an invalid escape sequence, Java will generate a compilation error.
2. Can I use escape sequences in character literals?
Yes, you can use escape sequences in character literals. For example: char ch = ‘\n’;
3. Can I use escape sequences in numeric literals?
No, you cannot use escape sequences in numeric literals.
4. How do I represent a Unicode character in a string?
To represent a Unicode character in a string, you can use the escape sequence \u followed by the hexadecimal representation of the character. For example: String str = "\u00a9 Copyright";
5. What is the difference between a newline character and a carriage return character in Java?
A newline character (\n) moves the cursor to the next line, while a carriage return character (\r) moves the cursor to the beginning of the current line.