Therefore length of a\".length() + \"b is 16. Hence the string literal whose length we are finding out is a\".length() + \"b. It will give a compilation error because \" is taken as an escape sequence character and hence the string literal needs another double quotes to be properly closed.Īnswer is 16. Question 3: If \u0022 is the Unicode escape for double quote ("), then what will the below line print: Question 1: What will ("\".length()) give? \\ - Insert a backslash character in the text at this point.Īn escape sequence is a single character.
\" - Insert a double quote character in the text at this point. \' - Insert a single quote character in the text at this point. \f - Insert a formfeed in the text at this point. \r - Insert a carriage return in the text at this point. \n - Insert a newline in the text at this point. \b - Insert a backspace in the text at this point. \t - Insert a tab in the text at this point. To print the sentence: She said "Hello!" to me.
For example, if you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. When an escape sequence is encountered in a print statement, the compiler interprets it accordingly. In Java, a character preceded by a backslash (\) is an escape sequence and has special meaning to the java compiler.
Escape characters (also called escape sequences or escape codes) in general are used to signal an alternative interpretation of a series of characters.