English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In Java, you can define
char c = '\u4f60'; char m = '\u0045'; char e = '\u554a';
Such literals, for example:
System.out.println("\u535a\u5ba2\u56ed");
Such code will not have problems with Chinese garbled text in any encoding environment
But you cannot define such a literal:
char c = '\u000a'; char m = '\u0027';
This is because\u000a and \u0027are special escape characters, and Java does not provide any special handling for Unicode escape characters in string literals. The program will directly convert them to the characters they represent [JLS] 3.2]。
\u000a is a LineFeed, which is a newline, so the program will be compiled into
char c = ' ';
Of course, it is a compilation error
Another example is:
System.out.println("a\u0022.length()+\u0022b".length());
A very superficial analysis of the program would suggest that it should print out26,a slightly deeper analysis would suggest that the program should print16,if you actually run it, and find that the result is neither26也不是16,而是2.
Because, \u0022It is the escape character of double quotes, and the program will be compiled as
String str = "a".length()+"b"; System.out.println(str.length());
Based on this case, I wrote an example, everyone can run and try the result
String str = "\u0061\u0022\u002b\u0028\u006e\u0065\u0077\u0020\u006a\u0061\u0076\u0061\u002e\u0075\u0074\u0069\u006c\u002e\u0063\u006f\u006e\u0063\u0075\u0072\u0072\u0065\u006e\u0074\u002e\u0043\u0061\u006c\u006c\u0061\u0062\u006c\u0065<\u0056\u006f\u0069\u0064>\u0028\u0029\u007b\u0070\u0075\u0062\u006c\u0069\u0063\u0020\u0056\u006f\u0069\u0064\u0020\u0063\u0061\u006c\u006c\u0028\u0029\u007b\u0074\u0068\u0072\u006f\u0077\u0020\u006e\u0065\u0077\u0020\u0052\u0075\u006e\u0074\u0069\u006d\u0065\u0045\u0078\u0063\u0065\u0070\u0074\u0069\u006f\u006e\u0028\u0022\u0073\u0075\u0070\u0072\u0069\u0073\u0065\u0020\u006d\u0061\u0074\u0068\u0065\u0072\u0020\u0066\u0075\u0063\u006b\u0065\u0072\u0021\u0022\u0029\u003b\u007d\u007d\u0029\u002e\u0063\u0061\u006c\u006c\u0028\u0029\u002b\u0022"; System.out.println(str);
That's all for this article. I hope the content of this article can bring some help to everyone's learning or work, and I also hope to support the Yelling Tutorial more!
Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (When reporting via email, please replace # with @) for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.