Pages

Friday, December 3, 2021

Java Escape Sequences

1. Overview

In this tutorial, We'll learn what are the escape sequences in java? 

And also we will learn the following.

Why do we need to use these special sequences and their importance? 
how many escape characters are supported by Java? (escape sequences list)
What is the length of the special escape sequence?
How to handle Octal and Unicode characters?
Various examples of escape characters.


Java Escape Sequences



2. What is the meaning of Escape Sequence in Java?


If the character is preceded by a backslash then it is called an Escape sequence.

what is the importance of it?


If we want to send some special instructions to the JVM, we need to use the escape character with the backslash.

All escape sequences have a special meaning to the compiler.


3 Escape Sequences List


how many escape characters are supported by Java?

Java supports 8 types of Escape characters as below.

Escape Sequence --> Description
\t Inserts a tab in the text at this point.
\b Inserts a backspace in the text at this point.
\n Inserts a new line in the text at this point.
\r Inserts a carriage return in the text at this point.
\f Inserts a form feed in the text at this point.
\' Inserts a single quote character in the text at this point.
\" Inserts a double quote character in the text at this point.
\\ Inserts a backslash character in the text at this point.



4. What is the length of the special escape sequence?


When we look at any escape character, it has a total of two characters. One is backslash and the other is an escape character.

Create a simple string with \n escape character and call length() method on the string.

Example 1
package com.javaprogramto.programs.escape.sequences;

public class EscapeSequencesExample1 {

	public static void main(String[] args) {
		
		String newLineEscapeSequence1 = "\n";
		System.out.println("\\n length is "+newLineEscapeSequence1.length());
		

		String newLineEscapeSequence2 = "\n\n";
		System.out.println("\\n\\n length is "+newLineEscapeSequence2.length());
		

		String tabEscapeSequence1 = "\t";
		System.out.println("\\t length is "+tabEscapeSequence1.length());
		

		String backslashEscapeSequence1 = "\\";
		System.out.println("\\\\ length is "+backslashEscapeSequence1.length());

	}
}

Output
\n length is 1
\n\n length is 2
\t length is 1
\\ length is 1
"\n" length is coming as 1 and "\n\n" length is coming as 4. So every escape combination is a 1 character to the jvm.

So always the length of the any escape sequence is 1.

And also you can observer how the "\\" used with new line character n as "\\n" because  "\\" is a special escape character to print it as backslash we need to escape it with backslash "\" as shown in the abvoe example system.out.println()



5. How to handle Octal and Unicode characters in java with escape sequence?


A character literal can be expressed as an octal escape sequence in the form "\nnn".

n is an octal digit (0-7). The range for the octal escape sequence is from '\000' to '\377'.

The octal number 377 which is the same as the decimal number 255.

Using octal escape sequence, we can represent characters whose Unicode code range from 0 to 255 decimal integers.

Uniccode escape character is in the form of /uXXXX. Here XXXX is ranging from /u0000 to /uFFFF.

Example 2

The below example show both octal and unicode escapes.
package com.javaprogramto.programs.escape.sequences;

public class EscapeSequencesExample2 {

	public static void main(String[] args) {

		// octal escape sequence examples
		String octalEscape1 = "\52";

		System.out.println("\\52 octal represntation is " + octalEscape1);

		String octalEscape2 = "\53";

		System.out.println("\\53 octal represntation is " + octalEscape2);
		
		// unicode escape sequence examples

		String unicodeEscape1 = "\u5222";

		System.out.println("\\u5222 unicode represntation is " + unicodeEscape1);

		String unicodeEscape2 = "\u5333";

		System.out.println("\\u5333 unicode represntation is " + unicodeEscape2);
	}
}

Output
\52 octal represntation is *
\53 octal represntation is +
\u5222 unicode represntation is 刢
\u5333 unicode represntation is 匳


6. Java Escape Sequences Exmples


As we seen in the section 3, all escape characters supported by java. Let us write the simple exmaple to use all of these one by one.

Example 3
package com.javaprogramto.programs.escape.sequences;

public class EscapeSequencesExample3 {

	public static void main(String[] args) {

		// newline \n escape sequence
		String newlineEscape = "hello\nworld";
		System.out.println("\nnewline : ");
		System.out.println(newlineEscape);

		// tab \t escape sequence
		String tabEscape = "hello\tworld";
		System.out.println("\ntab : ");
		System.out.println(tabEscape);

		// backspace \b escape sequence
		String backspaceEscape = "hello\bworld";
		System.out.println("\nbackspace : ");
		System.out.println(backspaceEscape);

		// carriage \r escape sequence
		String carriageEscape = "hello\rworld";
		System.out.println("\ncarriage : ");
		System.out.println(carriageEscape);

		// form feed \f escape sequence
		String formfeedEscape = "hello\fworld";
		System.out.println("\nform feed : ");
		System.out.println(formfeedEscape);

		// single quote \' escape sequence
		String singlequoteEscape = "hello\'world";
		System.out.println("\nsingle equote : ");
		System.out.println(singlequoteEscape);

		// double quote \" escape sequence
		String doublequoteEscape = "hello\"world";
		System.out.println("\ndouble quote : ");
		System.out.println(doublequoteEscape);

		// backslash \\ escape sequence
		String backslashEscape = "hello\\world";
		System.out.println("\nbackslash : ");
		System.out.println(backslashEscape);
	}
}

Output
newline : 
hello
world

tab : 
hello	world

backspace : 
helloworld

carriage : 
hello
world

form feed : 
helloworld

single equote : 
hello'world

double quote : 
hello"world

backslash : 
hello\world


7. Conclusion


In this article, We've seen the escape sequenceas in java.


No comments:

Post a Comment

Please do not add any spam links in the comments section.