A unicode newline character(\u000d) in Java

JavaUnicode

Java Problem Overview


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 Solutions


Solution 1 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLionView Question on Stackoverflow
Solution 1 - JavaSLaksView Answer on Stackoverflow