\ u in Java comment leads to error message, why?

// \u represent unicode sequence char c = '\u0045'; System.out.println(c); 

Only this code, and eclipse shows the following error message

 "Exception in thread "main" java.lang.Error: Unresolved compilation problem: Invalid unicode 

Now when I remove \u from the comment, it works fine, what is the problem with \u in the comment? This is a mistake or there is some relation, because I think that Java should leave comments as is.

+8
java unicode
source share
2 answers

The compiler will read all of your source code first (the comment will be ignored later), but it cannot recognize "\ u" because it is not a valid Unicode character.

To fix this, you can write

 // \\u represent unicode sequence (extra backslash for escaping) 

or

 // \ u represent unicode sequence (extra whitespace for escaping) 

Edit:
This is because the compiler first translates everything to Unicode. Writing this simple program

 class Ideone { public static void main (String[] args) { // \u000A System.out.println("Hi"); } } 

Hello displays, because \u000A denotes a new line. Evidence

+12
source share

Comments are also interpreted to some extent. A brief overview led to your question the following result: Unicode in javadoc and comments?

+5
source share

All Articles