Escape Sequence in Java | Example Program

An escape sequence in Java is a special type of character literal. It is generally used to control printed or displayed output.

A character interpreted with backslash (\) is called escape sequence or escape character in Java. It is used to perform some specific task.

Let’s understand it with the help of an example.

Suppose we want to display a message with quotation marks in the output. Can we write a statement to print a message on the console like this:

System.out.println("He said "Java is fun unlimited"");

No, we cannot write this statement to print message because the compiler will generate compile-time error.

Java compiler will think that the second quotation character is the end of the string and does not know what to do with the rest of the characters.

To overcome it, Java introduces a special notation to represent special characters. This special notation is called escape sequence. It consists of a backslash (\) followed by a character or a combination of digits.

For example, \t is an escape sequence for the Tab character. \u0009 is a Unicode to represent Tab character. \” is an escape sequence for double quotes. \u0022 is Unicode for it.

The symbols used in an escape sequence are interpreted as a whole rather than individually. An escape sequence is known as a single character.

Let’s take an example program where we will print the quoted message using escape sequence.

Program source code 1:

public class A {
public static void main(String[] args) { 
	
 System.out.println("He said \"Java is fun unlimited\"");
  }
}
Output:
      He said "Java is fun unlimited"

Note: The symbols \ and ” together represent a single character.

Java Escape Sequences Table form


There is a total of ten escape sequences or character sequences in Java. They are listed in the below table form:

Escape Sequence Name
\b
Backspace
\t
Horizontal Tab
\n
Line feed (New line)
\f
Form feed
\r
Carriage Return 
\\
Backslash
\”
Double Quote
\’
Single Quote 
\DDD
Octal Character
\uHHHH
Unicode Character 

Example Program based on Escape Sequence Characters


Let’s take some important example programs based on Java escape sequence characters.

a) Program based on the escape sequence \t:

public class Test {
public static void main(String[] args) 
{ 
// \t: It gives a tab between two words.
  System.out.println("Good Morning\tScientech Easy ");
  }
}
Output:
      Good Morning	Scientech Easy

b) Program based on escape sequence character \n:

public class Test {
public static void main(String[] args) 
{ 
// \n: This escape sequence is for a new line.
  System.out.println("Good Morning \nScientech Easy ");
  System.out.print("*\n**\n***\n");
  }
}
Output:
      Good Morning 
      Scientech Easy 
      *
      **
      ***

c) Program based on escape sequence \r:

public class Test {
public static void main(String[] args) 
{ 
// \r: It moves the output point back to the starting of the line without moving down a line (generally).
  System.out.println("Good Morning \r Scientech Easy ");
  }
}
Output:
       Good Morning 
         Scientech Easy

d) Program based on escape sequence \’:

public class Test {
public static void main(String[] args) 
{ 
// \': It is used for printing a single quotation mark on the text string.
  System.out.println("Good Morning \'Scientech Easy\'");
  }
}
Output:
      Good Morning 'Scientech Easy'

e) Program based on escape sequence \”:

public class Test {
public static void main(String[] args) 
{ 
// \":  It is used for printing a double quotation mark on the text string.
  System.out.println("Good Morning \"Scientech Easy\"");
  }
}
Output:
       Good Morning "Scientech Easy"

f) Program based on escape sequence \\:

public class Test {
public static void main(String[] args) 
{ 
// \\: It is used for printing a backslash on the text string.
  System.out.println("Good Morning \\Scientech Easy\\");
  }
}
Output:
      Good Morning \Scientech Easy\

Hope that this tutorial has covered almost all the important points related to escape sequence in Java with example program. I hope that you will have understood the basics of escape sequence characters.
Thanks for reading!!!
Next ⇒ Classes and Objects in Java

⇐ Prev Next ⇒