Escape Sequence

What is Escape sequence?

In Java, an escape sequence is a combination of characters that represents a special character when used within a string. It begins with a backslash (‘\’) and is followed by one or more characters. Escape sequences are used to include special characters in strings, such as newline characters (\n), tab characters (\t), or quotation marks (\”). They enable the representation of characters that would otherwise be difficult or impossible to include directly in a string.

List of Escape Sequences:

 

1. \n : Inserts a newline:

There are several types of escape sequence that we use in Java. Some of them are discussed are: \n This escape sequence is used to define a new line. So that we do not need to write System.out.print again and again.

Example:
System.out.println("\"java\nis\nan\ninteresting\nlanguage\"");
Output:

“java

is

an

interesting

language”

Explanation:
  • The outermost double quotes ” serve as delimiters to signify the start and end of the string to be displayed.
  • Inside the string, we have the following escape sequences : \n This represents a new line, so when Java encounters \n, it moves to the next line before printing the rest of the string. So, the output will have each word in a separate line.
2. \t : Inserts a tab:

\t This escape sequence is used print  tab in console.

Example:
System.out.println("Hi!");
System.out.println("\tthere");
Output:

Hi

there

Explanation:
  • The outermost double quotes ” are used to indicate the beginning and end of the string that will be printed.
  • Inside the string, we have the following escape sequence: \t This represents a horizontal tab character. When Java encounters \t, it adds a tab spacing before printing the rest of the string.
3. \” : Inserts a double quote:

\” We use this character to give double  quotation.

Example:
System.out.println("\"hello world\"");
Output:
“hello world”
Explanation:
  • The outermost double quotes ” serve as delimiters to signify the start and end of the string to be displayed.
  • Inside the string, we have the following escape sequence: \” This represents a double quote character (“). Since we are already using double quotes to define the string, we need to escape the inner double quotes to include them in the output.
4. \’ : Inserts a single quote:

\’  We use this character to give single  quotation.

Example:
System.out.println("\'Programming\' is fun");
Output:
‘Programming’ is fun
Explanation:
  • The outermost double quotes ” serve as delimiters to signify the start and end of the string to be displayed.
  • Inside the string, we have the following escape sequence: \’ This represents a single quote character (‘). Since we are using single quotes to define the string, we need to escape the inner single quotes to include them in the output.
5. \\: Inserts a backslash:

\\  This character is used to print a backslash on text string.

Example:
System.out.println("Programming\\fun");
Output:
Programming\fun
Explanation:
  • If you use the code System.out.println(“Programming\\fun”); in a Java program, it will print the following output: Programming\fun
  • In this case, the backslash “\\” is an escape character in Java. When you use “\\” in a string literal, it represents a single backslash character in the output. So the output displays “Programming\fun” with only one backslash.
6.\r: carriage return:

\r  This character is used to print a carriage return character on text string.

Example:
System.out.println( "Programmining\rfun" );
Output:

Programming

fun

7. \f: form feed:

\f  This \f escape sequence is a form feed character. It is an old technique and is used to indicate a page break.

Example:
System.out.println("Good Morning Mariya! \f How are you?");
Output:

Good Morning Mariya!  How are you?

8. \b: Inserts a backspace:

\b This character is used to print a backspace on text string.

Example:
System.out.println("Good Morning\b Mariya! ");
Output:

Good Morning Mariya!

Implement of Escape Sequence:
public class EscapseSequenceExample {

    public static void main(String[] args) {
        System.out.println("Hello!\nwelcome to programming world");
        System.out.println("\t\'Programming\' is fun");
        System.out.println("\\\"Java\"is on of the best programming language.\\");
    }
}
Output:

Hello!

welcome to programming world

‘Programming’ is fun

\”Java”is on of the best programming language.\

Visited 1 times, 1 visit(s) today

Comments are closed.

Close