I am very new to Java and I have code like this:
public class Puzzle {
public static void main(String... args) {
System.out.println("Hi Guys!");
// Character myChar = new Character('\u000d');
}
}
You can see the line:
Character myChar = new Character('\u000d');
is commented out. But still, I get an error like this when I run javac
:
Puzzle.java:9: error: unclosed character literal
// Character myChar = new Character('\u000d');
^
1 error
In this blog post I found the reason for the exception. The blog says:
Java compiler, just before the actual compilation strips out all the unicode characters and coverts it to character form. This parsing is done for the complete source code which includes the comments also. After this conversion happens then the Java compilation process continues.
In our code, the when Java compiler encounters \u000d, it considers this as a newline and changes the code as below,
public class Puzzle {
public static void main(String... args) {
System.out.println("Hi Guys!");
// Character myChar = new Character('
');
}
}
With this I have two questions:
- Why does Java parse the unicode first? Are there any advantages to it?
- Because the line is still commented, Java is trying to parse it! Is this the only case it does? Or does it generally parse the commented lines too? I'm confused.
Thanks in advance.