An bug for escape characters in java

  • 2020-06-07 04:30:17
  • OfStack

In java, you can define

char c = '\u4f60';
char m = '\u0045';
char e = '\u554a';

For example:


No matter what the code in what coding environment will not appear Chinese random code problem

But you can't define a literal like this:

char c = '\u000a';
char m = '\u0027';

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

\u000a is 1 LineFeed, that is, a newline so that the program compiles to

char c = '

Naturally, there was a compilation error

Another example is:


A very superficial analysis of the program would say it should print 26, a slightly deeper analysis of the program would say it should print 16, and if you actually run it once, it turns out to be neither 26 nor 16, but 2.

Because \u0022 is an escaped word in double quotes, the program will eventually compile to

String str = "a".length()+"b";

Based on this example, I've written an example where you can run trial 1

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";

Related articles: