A bug about escape characters in Java

In Java, you can define

Such literal quantity, for example:

System. out. println("\u535a\u5ba2\u56ed");

No matter what coding environment, such code will not have the problem of Chinese random code

But you can't define such a literal quantity:

This is because \ u000a and \ u0027 are special escape characters. Java does not provide any special treatment for Unicode escape characters in string literal constants. The program will directly convert the original characters into the characters they represent [JLS 3.2].

\U000a is a linefeed, that is, line feed, so that the program will be compiled into

Of course, there is a compilation error

Another example is:

System. out. println("a\u0022.length()+\u0022b". length());

A very superficial analysis of the program will think that it should print 26. A little deeper analysis will think that the program should print 16. If you actually run it again, you find that the result is neither 26 nor 16, but 2

Because \ u0022 is an escape word in double quotation marks, the program will eventually compile into

Based on this case, I wrote an example. You can run it and try the results

The above is the whole content of this article. I hope the content of this article can bring some help to your study or work. At the same time, I also hope to support a lot of programming tips!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>