为什么 int j = 012 给出输出 10?

2022-01-14 00:00:00 numbers integer literals java

在我的实际项目中偶然发生的这里是我修改后的小程序.

In my actual project It happened accidentally here is my modified small program.

我不明白为什么它会输出 10?

I can't figure out why it is giving output 10?

public class Int
{
    public static void main(String args[])
    {
        int j=012;//accidentaly i put zero 
        System.out.println(j);// prints 10??
    }
}

在那之后,我放了两个零,仍然给出输出 10.

After that, I put two zeros still giving output 10.

然后我将 012 更改为 0123,现在输出 83?

Then I change 012 to 0123 and now it is giving output 83?

谁能解释一下原因?

推荐答案

比我把 012 改成 0123 现在输出 83 吗?

Than I change 012 to 0123 and now it is giving output 83?

因为,它被视为八进制基数 (8),因为该数字的前导是 0.所以,它对应的十进制值为10.

Because, it's taken as octal base (8), since that numeral have 0 in leading. So, it's corresponding decimal value is 10.

012:

(2 * 8 ^ 0) + (1 * 8 ^ 1) = 10

0123:

(3 * 8 ^ 0) + (2 * 8 ^ 1) + (1 * 8 ^ 2) = 83

相关文章