简体   繁体   中英

Why isn't “\400” a compile-time error?

Character values between 0 and 255 can be denoted by octal literals from "\\000" to "\\377" .

So shouldn't "\\400" be a compile-time error? Eclipse does not complain, however... what's going on here?

It's interpreting it as "\\40" + "0"

The Java Language Specification describes this here .

OctalEscape:
    \ OctalDigit
    \ OctalDigit OctalDigit
    \ ZeroToThree OctalDigit OctalDigit

OctalDigit: one of
    0 1 2 3 4 5 6 7

ZeroToThree: one of
    0 1 2 3

It falls under the construction of

\ OctalDigit OctalDigit

... followed by '0'. It doesn't fall under

\ ZeroToThree OctalDigit OctalDigit

... so it's not ambiguous or out of range. See section 3.10.6 of the Java Language Specification for more details.

Note that you can't use it as a character literal for exactly this reason:

char x = '\377'; // Fine
char y = '\400'; // Error: unclosed character literal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM