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:
  1. The outermost double quotes “mark the beginning and conclusion of the string that will be shown and act as delimiters.
  2. 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:

\” This character is used to provide a double quotation.

Example:
System.out.println("\"hello world\"");
Output:
“hello world”
Explanation:
  • The outermost double quotes “mark the beginning and conclusion of the string that will be shown and act as delimiters.
    We have the following escape sequence within the string: \”This represents a double quote character (” ). To include the inner double quotes in the output, we must escape them as we are already using them to define the string.
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 and indicate the start and end of the string that will be shown.
    The string contains the following escape sequence: This is a single quote character (‘). Since we are using single quotes to define the string, we must escape them in order for the output to have the inner single quotes.
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