Why does this code showing error invalid unicode?

//System.out.println("hii");'uxxx'

println语句被评论,但unicode没有评论。为什么?


Java allows you to use Unicode in your source code. Unlike many other languages, it allows you to do so anywhere, including, of course, comments. And it allows it in identifiers as well, so you can write legal Java code like this:

    String हिन्दी = "Hindi";

The variable name is perfectly legal (although coding conventions discourage such use).

So as far as javac is concerned, the source code is Unicode. The problem is that it can be represented with different encodings, and some editors don't support Unicode, and there are places where using a non-ASCII file is going to create problems.

So it is allowed to use Unicode escapes in the code. This will make the file be entirely in ASCII despite having identifiers or comments in Unicode. You can replace any character in the code with the equivalent Unicode escape. Even the "normal" characters like ; . For example, the following line:

String s = "123";

Can be written as:

String s u003d "123"u003b

And it will be compiled correctly and without any problems. You can, in fact, write the whole program in Unicode escapes, including the newlines. The Java compiler simply doesn't care if the Unicode escapes are inside literals or in the source itself.

But the upshot of this is that the compiler needs to interpret Unicode escapes first, and only then break the source into tokens such as identifiers, operators and comments, and after that it checks syntax etc.

Which means that if you have an illegal Unicode escape sequence in your source, it will be flagged as an error even though it's inside a comment , because at this point the compiler doesn't even know that the particular part of the code it is looking at is a comment.


Unicode can be represented with uCODE and not /uCODE . If your unicode is new line and you try to write something after unicode it may show you compile time error.Otherwise inline unicodes are commented in single line comment.No need to specifically comment unicode.

//Compilation Error
//System.out.println("hii"); u000d Hello

EDIT

When compiler starts it replaces all unicode character with it's value including the characters of comment.

So in above statement during compilation it becomes.

//System.out.println("hii");
Hello

When the specification for the Java language was created, the Unicode standard was accepted and the char primitive was defined as a 16-bit data type, with characters in the hexadecimal range from 0x0000 to 0xFFFF.

Also you should use "001" instead of "/0001".

链接地址: http://www.djcxy.com/p/20574.html

上一篇: 为什么一些字符文字会导致Java中的语法错误?

下一篇: 为什么此代码显示错误无效的Unicode?