Escape sequences in Java


Escape sequences are a unique kind of character that are used to indicate a different way of interpreting a group of characters. An escape sequence in Java is a character that is preceded by a backslash (). An escape sequence is treated by the Java compiler as a single character with unique meaning.

Java frequently employs the following escape sequences:

  • \t: Adds a new tab


  • : Adds a new line

  • \r: Adds a carriage return

  • ': Adds a single quote

  • ": Adds a double quote

  • \: Adds a backslash

These escape sequences can be used to control the output of text in Java programs.

Java escape characters examples

  • Depending on escape sequence \t

  • Depending on escape sequence

  • Depending on escape sequence \b

  • Depending on escape sequence \r

  • Depending on escape sequence \f

  • Depending on escape sequence \’

  • Depending on escape sequence \”

Example 1: Depending on escape sequence \t

The code employs the System.out.println statement to display a message. The message contains "Hello Everyone" and "Welcome" separated by a tab character (\t). It creates a gap between the words when printed.

Algorithm

  • Step 1: Create a class called TLP.

  • Step 2: Inside the TLP class, create a method called main().

  • Step 3: Within the main() method, declare a string variable called message and set it to the value "Hello Everyone\tWelcome ".

  • Step 4: Employ the System.out.println() method to print the value of the message variable.

  • Step 5: The \t escape sequence in the message variable will let a tab be inserted between the words "Hello Everyone" and "Welcome".

  • Step 6: The System.out.println() method will print the output.

Example

public class TLP {
public static void main(String[] args) 
{ 
// \t: It offers a tab between two words.
  System.out.println("Hello Everyone\tWelcome ");
  }
}

Output

Hello Everyone	Welcome 

Example 2: Depending on escape sequence

The provided code employs the System.out.println statement to display a message. The message includes "Hello Everyone!" and "Welcome to tutorialspoint" with a
escape sequence in between. This sequence makes the second part of the message appear on a new line when printed.

Algorithm

  • Step 1: In the main() method, declare a string variable called message. Set it to the value. In this example we have taken "Hello Everyone!
    Welcome to tutorialspoint”

  • Step 2: Employ the System.out.println() method to print the value of the message variable.

  • Step 3: The
    escape sequence in the message variable helps a new line to be inserted after the words "Hello Everyone!".

  • Step 4: Print the output

Example

// 
escape sequence is employed for a new line. public class TLP { public static void main(String[] args) { System.out.println("Hello Everyone!
Welcome to tutorialspoint "); } }

Output

Hello Everyone! 
 Welcome to tutorialspoint  

Example 3: Depending on escape sequence \b

The provided code utilizes the System.out.println statement to display a message. The message includes "Hello World" followed by the \b escape sequence and "Good Evening!". The \b escape sequence acts as a backspace character, which in some cases can either erase the character before it or move the cursor one step back.

Algorithm

  • Step 1: Define a public class named TLP.

  • Step 2: Within the main method's curly braces, employ System.out.println() to output text on the console.

  • Step 3: In the text content within the System.out.println() parentheses, include the phrase "Hello World\b Good Evening!"

  • Step 4: \b represents the backspace escape sequence.

Example

// The escape sequence \b is a backspace character
//Depending on the compiler, it either deletes the character or shifts the cursor back one character.
public class TLP {
	public static void main(String[] args)
	{
		System.out.println("Hello World\b Good Evening! ");
	}
}

Output

Hello World Good Evening! 

Example 4: Depending on escape sequence \r

The given code employs the System.out.println statement to display a message. The message contains "Welcome Students" followed by the \r escape sequence and "to tutorialspoint."

Algorithm

  • Step 1: Within the main() method, declare a string variable called message and set it to the value "Welcome Students \r to tutorialspoint ".

  • Step 2: Employ the System.out.println() method to print the value of the message variable.

  • Step 3: The \r escape sequence in the message variable will move the cursor to the beginning of the line, without printing a new line.

  • Step 4: Print the output.

Example

public class TLP {
public static void main(String[] args) 
{ 
// \r: Without generally progressing along a line, it returns the output point to the line's beginning.

  System.out.println("Welcome Students \r to tutorialspoint ");
  }
}

Output

Welcome Students 
 to tutorialspoint  

Example 5: Depending on escape sequence \f

In the code below, the message includes "Hello Everyone!" followed by the \f escape sequence and "Are you all doing good?". The \f escape sequence is used to signify a page break, but its effect might not be visible in all environments.

Algorithm

  • Step 1: In the main() method, declare a string variable called message and set it to the value "Hello Everyone! \f Are you all doing good? ".

  • Step 2: Employ the System.out.println() method to print the value of the message variable.

  • Step 3: The \f escape sequence in the message variable will insert a form feed, which will cause the output to be moved to the top of the next page.

  • Step 4:l Print the output

Example

 // \f escape sequence is a form feed character used to indicate a page break.

public class TLP {
	public static void main(String[] args)
	{
		System.out.println("Hello Everyone! \f Are you all doing good? ");
	}
}

Output

Hello Everyone!  Are you all doing good? 

Example 6: Depending on escape sequence \’

The code is a Java program that uses the System.out.println statement to display a message. The message contains "Hello this is 'Tutorialspoint'" with the ' escape sequence used to print a single quotation mark within the text.

Algorithm

  • Step 1: In the main() method, declare a string variable called message. Then set it to the value "Hello this is 'Tutorialspoint'".

  • Step 2: Employ the System.out.println() method to print the value of the message variable.

  • Step 3: The ' escape sequence in the message variable will tell the compiler that the single quotation mark is part of the string, and not the end of the string.

  • Step 4: Print the output.

Example

public class TLP {
public static void main(String[] args) 
{ 
// '' is employed to print a single quotation mark on the text string.
  System.out.println("Hello this is 'Tutorialspoint'");
  }
}

Output

Hello this is 'Tutorialspoint'

Example 7: Depending on escape sequence \”

Algorithm

  • Step 1: In the main() method, declare a string variable called messag. Set it to the value "Hello World "This is Tutorialspoint"".

  • Step 2: Employ the System.out.println() method to print the value of the message variable.

  • Step 3: The " escape sequence in the message variable will tell the compiler that the double quotation mark is part of the string, and not the end of the string.

  • Step 4: Print the output.

Example

public class TLP {
public static void main(String[] args) 
{ 
// " is used to print a double quotation mark on the text string.
  System.out.println("Hello World "This is Tutorialspoint"");
  }
}

Output

Hello World "This is Tutorialspoint"

Conclusion

Escape sequences are a special type of character that is used to signal an alternative interpretation of a series of characters in Java. They are often used to control the output of text in Java programs.

Some commonly used escape sequences in Java are \t,
, \r, ', ", and \. The escape sequences can be employed for various operations like insert tabs, new lines, carriage returns, single quotes, double quotes, and backslashes into text.

Updated on: 29-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements