Java Reference
In-Depth Information
Unicode escapes in the original program are replaced with this escape sequence, it will print 16 as
expected:
System.out.println("a\".length() + \"b".length());
There are escape sequences for many characters, including the single quote ( \' ), linefeed ( \n ), tab
( \t ), and backslash ( \\ ). You can use escape sequences in character literals as well as in string
literals. In fact, you can put any ASCII character into a string literal or a character literal by using a
special kind of escape sequence called an octal escape , but it is preferable to use normal escape
sequences where possible. Both normal escape sequences and octal escapes are far preferable to
Unicode escapes because unlike Unicode escapes, escape sequences are processed after the
program is parsed into tokens.
All the programs in this topic are written using the ASCII subset of Unicode. ASCII is the lowest
common denominator of character sets. ASCII has only 128 characters, but Unicode has more than
65,000. A Unicode escape can be used to insert any Unicode character into a program using only
ASCII characters. A Unicode escape means exactly the same thing as the character that it
represents.
Unicode escapes are designed for use when a programmer needs to insert a character that can't be
represented in the source file's character set. They are used primarily to put non-ASCII characters
into identifiers, string literals, character literals, and comments. Occasionally, a Unicode escape
adds to the clarity of a program by positively identifying one of several similar-looking characters.
In summary, prefer escape sequences to Unicode escapes in string and character literals.
Unicode escapes can be confusing because they are processed so early in the compilation sequence.
Do not use Unicode escapes to represent ASCII characters. Inside of string and character literals,
use escape sequences; outside of these literals, insert ASCII characters directly into the source file.
< Day Day Up >
 
 
Search WWH ::




Custom Search