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

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

I stumbled over this (again) today:

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

It

5条回答
  •  Happy的楠姐
    2020-12-13 13:05

    It is described in 3.3. Unicode Escapes http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html. Javac first finds \uxxxx sequences in .java and replaces them with real characters then compiles. In case of

    char error = '\u000A';
    

    \u000A will be replace with newline character code (10) and the actual text will be

    char error = '
    ';
    

提交回复
热议问题