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

北城以北 提交于 2020-08-12 15:14:51

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!