54

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?

dimo414
  • 47,227
  • 18
  • 148
  • 244
Lion
  • 18,729
  • 22
  • 80
  • 110

1 Answers1

58

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.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    \u000d is for CR (Carriage Return) and \u000a is for LF (Line Feed) https://en.wikipedia.org/wiki/List_of_Unicode_characters – ezzadeen Jul 06 '19 at 21:26