A unicode newline character(\u000d) in Java

2019-01-07 06:34发布

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 unicode
1条回答
霸刀☆藐视天下
2楼-- · 2019-01-07 06:59

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.

查看更多
登录 后发表回答