The following code produces the output "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 \u000d
as a new line and gets transformed into:
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?
Update:
In my original question, I used IntelliJ Idea. And like @dhke pointed out, IntelliJ got it wrong. However, I noticed that Idea 2024.1.1 addresses the issue directly. The formatting indicates that it is not a comment. Additionally, when auto-formatting, it will change \u000d to an actual new line. (I am not sure when this issue was addressed)