Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

\u in Java comment is leading to error message, why?

Tags:

java

unicode

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

Code is only this much and eclipse is showing following error message

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

Now when I remove \u from comment it is working fine, what is the problem with \u in comment? IS this a bug or there is some relation, as far as think Java should leave the comments as it is.

like image 661
Raj Avatar asked May 27 '14 12:05

Raj


1 Answers

The compiler will read your entire source code first (the comment will be ignored later), but he can't recognize "\u ", because it is no valid unicode-character.

To fix it, 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 translates everything into unicode first. Writing this simple programm

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

outputs Hi, because \u000A stands for newline. Proof

like image 184
Manuel Allenspach Avatar answered Sep 27 '22 19:09

Manuel Allenspach