在一个测验中,我发现了一个问题,我需要用下面的程序计算
public static void main(String[] args) {
short x = 0;
int b = 08;
x +=b;
System.out.println("" + b + x );
}
编译错误
int b = 08;
因为它是一个八进制值,所以我尝试了一些不同的值
int b = 07 // working fine (decimal of same is 7)
int b = 08 // (Decimal value 8) // Compilation error
int b = 09 // (Decimal value 9) // Compilation error
int b = 010 // (Decimal value 8) // No Compilation error
因为 08 和 010 具有相同的十进制数,那么为什么 08 会出现编译错误。
最佳答案
因为在八进制 表示法中,010
!= 08
。实际上,08
在八进制数系统 中不存在。您只能使用数字 0-7
(以 0
开头)。
关于java - 八进制值赋值给 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32645351/