Java 中的合法注释内容导致错误
在 此 coderanch 链接上,我发现下面的注释会给出编译器错误:-
// Compiler Error due to this Unicode char '\u000a'
原因是,Unicode 序列直接被它对应的实际字符替换。由于'\u000a'对应于newLine字符,因此在找到'\u000a'的地方放置一个newLine。
我的问题是,“是否还有其他方法因评论而出现编译错误?”
On this coderanch link, I found that the following comment will give compiler error :-
// Compiler Error due to this Unicode char '\u000a'
Reason being, the Unicode sequence is directly replaced by the actual character it corresponds to. Since '\u000a' corresponds to newLine character, a newLine is placed at the place where '\u000a' is found.
My question is that, "Is there any other way of having Compilation Error due to a comment?"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“编译器不仅在将程序解析为标记之前将 Unicode 转义转换为它们所表示的字符 [...],而且在丢弃注释和空格之前执行此操作 [JLS 3.2]。” Java™ Puzzlers: Traps, Pitfalls, and Corner Cases 作者:Joshua Bloch, Neal Gafter。
接下来的几行是有效的 Java 代码:
"The compiler not only translates Unicode escapes into the characters they represent before it parses a program into tokens [...], but it does so before discarding comments and white space [JLS 3.2]." Java™ Puzzlers: Traps, Pitfalls, and Corner Cases By Joshua Bloch, Neal Gafter.
And the next lines are valid Java code:
如果您在注释中定义了一个已弃用的函数 (
@deprecated
),并且您将编译器设置为在使用已弃用的方法时抛出错误(至少可以配置内部 Eclipse 编译器,AFAIK)IF you define a function a deprecated in a comment (
@deprecated
), AND you set your compiler to throw errors when deprecated methods are used (at least the internal Eclipse compiler can be configured this was, AFAIK)这个错误不是由评论本身引起的。如果您在代码中的其他地方使用相同的
\u000a
,您将收到相同类型的错误。例如:转义序列位于示例中的注释中这一事实并不意味着注释是导致错误的原因。
This error is not caused by the comment itself. If you use the same
\u000a
somewhere else in your code, you'll get the same kind of error. For example:The fact that the escape sequence is in a comment in your example doesn't mean that the comment is what causes the error.