Why can some ASCII characters not be expressed in the form '\uXXXX' in Java source code?

前端 未结 5 1611
被撕碎了的回忆
被撕碎了的回忆 2020-12-13 12:19

I stumbled over this (again) today:

class Test {
    char ok = \'\\n\';
    char okAsWell = \'\\u000B\';
    char error = \'\\u000A\';
}

It

5条回答
  •  孤城傲影
    2020-12-13 13:01

    Unicode escape sequences like \u000a are replaced by the actual characters they represent before the Java compiler does anything else with the source code. And so, your program eventually ends up at

    char ch = '
    ';
    

    So the \u000a in your source code is replaced internally by a linefeed character. Note that this happens before the compiler actually reads and interprets your source code.

    Referring to the Java Language Specification:

    It is a compile-time error for a line terminator (§3.4) to appear after the opening ' and before the closing '.

    And as well all know by heart, \n is a line terminator, quoting:

     LineTerminator:
        the ASCII LF character, also known as "newline"
        the ASCII CR character, also known as "return"
        the ASCII CR character followed by the ASCII LF character
    

    Other symbols that could cause problems are \, ' and " for example.

提交回复
热议问题