为什么在允许某些Unicode字符的注释中执行Java代码?

08/11 04:08
阅读数 26

问题:

The following code produces the output "Hello World!" 以下代码生成输出“Hello World!” (no really, try it). (不,真的,试试吧)。

public static void main(String... args) {

   // The comment below is not a typo.
   // \u000d System.out.println("Hello World!");
}

The reason for this is that the Java compiler parses the Unicode character \ as a new line and gets transformed into: 原因是Java编译器将Unicode字符\ 解析为新行并转换为:

public static void main(String... args) {

   // The comment below is not a typo.
   //
   System.out.println("Hello World!");
}

Thus resulting into a comment being "executed". 从而导致评论被“执行”。

Since this can be used to "hide" malicious code or whatever an evil programmer can conceive, why is it allowed in comments ? 由于这可以用来“隐藏”恶意代码或恶意程序员可以设想的任何东西, 为什么在评论中允许它

Why is this allowed by the Java specification? 为什么Java规范允许这样做?


解决方案:

参考一: https://stackoom.com/question/24vd5/为什么在允许某些Unicode字符的注释中执行Java代码
参考二: https://oldbug.net/q/24vd5/Why-is-executing-Java-code-in-comments-with-certain-Unicode-characters-allowed
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
在线直播报名
返回顶部
顶部