This tutorial explains what is Java Escape Characters or Escape Sequences and how to use them in a Java program:
In this tutorial, we will explore escape character sequences in Java. Here, you will get an introduction to Java escape sequences and a tabular format that covers major Java escape characters along with their description.
A Java program that covers most of these Java escape characters is included here and you can see how these Java escape characters behave according to their functionality.
Towards the end of the topic, we will take a look at the frequently asked questions that will help you understand the escaping characters in Java.
=> Visit Here To Learn Java From Scratch
Table of Contents:
Java Escape Characters / Sequences
A character when preceded by a “\” i.e. backslash (that adds specifies meaning to the compiler) is Java Escape Sequence or Escape Character. There are some commonly used Java escape characters that will be discussed in the next segment.
The Java compiler interprets this series of characters as a single character that has a certain meaning and based on that, the compiler returns the result.
List Of Escape Sequences In Java
Escape Sequence | Description |
---|---|
\t | Inserts a tab. |
\’ | Inserts a single quote. |
\’’ | Inserts a double quote. |
\r | It returns a carriage. |
\\ | Inserts a backslash. |
\n | Inserts a new line. |
\f | Inserts a form feed. |
\b | Inserts a backspace. |
\ddd | It represents an octal character. The octal escapes range from \0 to \377. |
\uxxxx | It represents a hexadecimal Unicode character. Here, \uxxxx represents \u0000 to \uFFFF. |
Using Escape Sequence in a Program
Given below is an example of the Java escape sequence. We have included some of the important escape sequences from the above list.
These escape sequences are self-explanatory or you can refer to the above description for each of these escape character’s functionality.
public class example { public static void main(String[] args) { // This will insert a Tab Space String str = "Saket\tSaurav"; System.out.println(str); // This will insert a New Line String str1 = "New\nLine"; System.out.println(str1); // This will insert a backslash String str2 = "Back\\Slash"; System.out.println(str2); // This will insert a Carriage String str3 = "Line\rBreak"; System.out.println(str3); // This will insert a single quote String str4 = "New\'Line"; System.out.println(str4); // This will insert a double quote String str5 = "New\''Line"; System.out.println(str5); } }
Output:
Frequently Asked Questions
Q #1) How do you write an escape sequence in Java?
Answer: These characters are preceded by a ‘\’. You can write the escape sequence with ‘\’ as a prefix and the meaningful character as a suffix.
Q #2) What is the Escape Sequence?
Answer: It is a sequence of characters that has special meaning to the compiler which translates these characters into some other characters. This sequence of characters is treated as a single character by the compiler.
Q #3) What does \r do in Java?
Answer: The escape sequence ‘\r’ returns a carriage or a line break in java. It moves the cursor to the beginning of the next line.
Q #4) How do you escape special characters in Java?
Answer: The special characters can also be used as an input and we can use Java escape characters/sequence on them. For example – if we use \n on the special characters, then it will be printed on a new line.
Q #5) How do you escape characters?
Answer: There are some characters that have special meaning to the compiler when we add a ‘\’ or backslash as a prefix.
Q #6) How do you skip a character in Java?
Answer: In Java, there are many loops as well as decision-making statements, break, continue statements that can be used to skip a particular character.
For example – You can traverse the characters of any String using a loop and then use an if-statement to skip a particular character.
Conclusion
In this tutorial, we have learned about Java escape sequences and we got familiar with some of the commonly used escape characters. These characters have a special meaning to the compiler and you can use them according to your convenience.
These escaping characters in Java are very useful while providing a new line, tab, backslash, quote, carriage, etc.
=> Check ALL Java Tutorials Here