Why does Java execute code hidden in comments via Unicode?
Hi everyone! I recently started learning Java and came across strange behavior. I'm writing simple code:
```java
public class Test {
public static void main(String[] args) {
// \u000d System.out.println("Hello from comment!");
System.out.println("Hello world");
}
}
```
I expected this code to output only "Hello world" because the second line is a comment. But when I run it, I get:
```
Hello from comment!
Hello world
```
How is this possible? I thought comments are completely ignored by the compiler. Why does code inside a comment get executed? Is this a compiler bug or is it intentional?
I googled it but couldn't find a clear explanation for a beginner. Please explain how this Unicode magic works. And is there any way to protect against such "surprises" in code?
No answers yet.
Log in to leave an answer