Java Reference
In-Depth Information
this one is true. Unfortunately, this one is a bit too true. The compiler not only translates Unicode
escapes into the characters they represent before it parses a program into tokens ( Puzzle 14 ), but it
does so before discarding comments and white space [JLS 3.2].
This program contains a single Unicode escape ( \u000A ), located in its sole comment. As the
comment tells you, this escape represents the linefeed character, and the compiler duly translates it
before discarding the comment. Unfortunately, this linefeed character is the first line terminator
after the two slash characters that begin the comment ( // ) and so terminates the comment [JLS 3.4].
The words following the escape ( is Unicode representation of linefeed (LF) ) are therefore
not part of the comment; nor are they syntactically valid.
To make this more concrete, here is what the program looks like after the Unicode escape has been
translated into the character it represents:
public class LinePrinter {
public static void main(String[] args) {
// Note:
is Unicode representation of linefeed (LF)
char c = 0x000A;
System.out.println(c);
}
}
The easiest way to fix the program is to remove the Unicode escape from the comment, but a better
way is to initialize c with an escape sequence instead of a hex integer literal, obviating the need for
the comment:
public class LinePrinter {
public static void main(String[] args) {
char c = '\n';
 
 
Search WWH ::




Custom Search