A unicode newline character(\u000d) in Java

Let's see the following code snippet in Java.

public class Main {
    public static void main(String[] args) {
        // new Character(' u000d System.out.println("Hello");
    }
}

In the above code, although the only line in the main() method is commented out, it displays the output Hello on the console, even though it looks like that this commented line contains some syntax errors. If this line is uncommented, it will not work at all, causing a compile-time error.

Why does it output "Hello" here?


Java parses character escape codes in source code, not just strings.
This allows you to use Unicode identifiers without a Unicode encoding.

Therefore, the u000d in the comment is parsed as a newline, ending the comment and beginning an instance initializer.


In fact it is one of the Java Puzzlers.

Puzzle 2: Line Printer

I really recommend reading the whole book.

链接地址: http://www.djcxy.com/p/20570.html

上一篇: 为什么Java允许在其标识符中使用控制字符?

下一篇: Java中的unicode换行符(\ u000d)