Cod*_*roc 10 java integer numbers literals
在我的实际项目中,这是偶然发生的,这是我修改过的小程序.
我无法弄清楚它为什么输出10?
public class Int
{
public static void main(String args[])
{
int j=012;//accidentaly i put zero
System.out.println(j);// prints 10??
}
}
Run Code Online (Sandbox Code Playgroud)
在那之后,我把两个零仍然给出输出10.
然后我将012更改为0123,现在它输出83?
有谁能解释为什么?
Abi*_*san 22
比我改变012到0123现在它给出输出83?
因为,它被视为八进制基数(8),因为该数字在前导中为0.所以,它的对应十进制值是10
012:
(2 * 8 ^ 0) + (1 * 8 ^ 1) = 10
Run Code Online (Sandbox Code Playgroud)
0123:
(3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83
Run Code Online (Sandbox Code Playgroud)