请问这里发生了什么?当我用前导零初始化整数时,程序不会忽略零,而是执行了其他我不知道的操作。
可能重复:
整数带有前导零
int num = 0200;
System.out.println(num); // 128
System.out.println(033); // 27
当然 - 根据Java语言规范第3.10.1节,它将其视为一个八进制字面量:
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 and can represent a positive, zero, or negative integer.
OctalNumeral: 0 OctalDigits OctalDigits: OctalDigit OctalDigit OctalDigits OctalDigit: one of 0 1 2 3 4 5 6 7
Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral-not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.
这是一个八进制(基数为8)字面量。
同样地,0x27
是一个十六进制(基数为16)字面量,它的十进制值为39。