Java Reference
In-Depth Information
A character in a string literal may appear literally or in the form of an escape
sequence. You can use Unicode escape sequences to represent any character. Some of
the characters such as line separator, carriage return, and so on cannot be represented
literally and they must appear as escape sequences. Table 4-4 lists the escape sequences
defined in Nashorn.
Table 4-4. Single Character Escape Sequences in Nashorn
Character Escape
Sequence
Unicode Escape
Sequence
Character Name
\b
\u0008
backspace
\t
\u0009
horizontal tab
\n
\u000A
line feed (new line)
\v
\u000B
vertical tab
\f
\u000C
form feed
\r
\u000D
carriage return
\”
\u0022
double quote
\'
\u0027
single quote
\\
\u005C
backslash
There is a significant difference in how Nashorn and Java interpret Unicode escape
sequences. Unlike Java, Nashorn does not interpret Unicode escape sequences as the
actual character before executing the code. The following single-line comment, character
literal, and string literal in Java will not compile because the compiler converts the \u000A
as a new line before compiling the program:
// This, \u000A, is a new line, making the single line comment invalid
char c = '\u000A';
String str = "Hello\u000Aworld!";
In Java, you must replace \u000A with \n in this code to work. The following snippet
of code in Nashorn works fine:
// This, \u000A, is a new line that is valid in Nashorn
var str = "Hello\u000AWorld!";
print(str);
Hello
World!
 
 
Search WWH ::




Custom Search