Error due to content in legal comment in Java

In this coderanch link, I found that the following comment will provide a compiler error: -

// Compiler Error due to this Unicode char '\u000a' 

The reason is that the Unicode sequence is directly replaced by the actual character to which it corresponds. Since "\ u000a" matches the newLine character, a new line is placed in the place where "\ u000a" is located.

My question is: "Is there any other way to have a compilation error due to comment?"

+1
source share
4 answers

IF you define a function deprecated in a comment ( @deprecated ) and you install a compiler to throw errors when deprecated methods are used (at least the internal Eclipse compiler can be configured, this is AFAIK)

+2
source

"The compiler not only translates Unicode escape codes into the characters they represent before it parses the program in tokens [...], but does it before discarding comments and spaces [JLS 3.2]." Java ™ Puzzlers: Traps, Traps, and Corner Cabinets by Joshua Bloch, Neil Gufter.

And the following lines are valid for Java code:

 \u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020\u0020 \u0063\u006c\u0061\u0073\u0073\u0020\u0055\u0067\u006c\u0079 \u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0020\u0020 \u0020\u0020\u0020\u0020\u0073\u0074\u0061\u0074\u0069\u0063 \u0076\u006f\u0069\u0064\u0020\u006d\u0061\u0069\u006e\u0028 \u0053\u0074\u0072\u0069\u006e\u0067\u005b\u005d\u0020\u0020 \u0020\u0020\u0020\u0020\u0061\u0072\u0067\u0073\u0029\u007b \u0053\u0079\u0073\u0074\u0065\u006d\u002e\u006f\u0075\u0074 \u002e\u0070\u0072\u0069\u006e\u0074\u006c\u006e\u0028\u0020 \u0022\u0048\u0065\u006c\u006c\u006f\u0020\u0077\u0022\u002b \u0022\u006f\u0072\u006c\u0064\u0022\u0029\u003b\u007d\u007d 
+4
source
 /* Compiler Error due to this Unicode char '*/' */ 
+1
source

This error is not caused by the comment itself. If you use the same \u000a somewhere else in your code, you will get the same error. For instance:

 // This will give you a similar error char c = '\u000a'; 

The fact that the escape sequence in a comment in your example does not mean that the comment is the cause of the error.

0
source

All Articles