Escape Characters OR Escape Sequences In Java

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

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

Java Escape Characters / Sequences

Java Escape Characters

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 SequenceDescription
\tInserts a tab.
\’Inserts a single quote.
\’’Inserts a double quote.
\rIt returns a carriage.
\\Inserts a backslash.
\nInserts a new line.
\fInserts a form feed.
\bInserts a backspace.
\dddIt represents an octal character. The octal escapes range from \0 to \377.
\uxxxxIt 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:

Java Escape Sequence example

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

Was this helpful?

Thanks for your feedback!

Leave a Comment