在我的实际项目中偶然发生的这里是我修改后的小程序.
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
这篇关于为什么 int j = 012 给出输出 10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
如何检测 32 位 int 上的整数溢出?How can I detect integer overflow on 32 bits int?(如何检测 32 位 int 上的整数溢出?)
return 语句之前的局部变量,这有关系吗?Local variables before return statements, does it matter?(return 语句之前的局部变量,这有关系吗?)
如何将整数转换为整数?How to convert Integer to int?(如何将整数转换为整数?)
如何在给定范围内创建一个随机打乱数字的 intHow do I create an int array with randomly shuffled numbers in a given range(如何在给定范围内创建一个随机打乱数字的 int 数组)
java的行为不一致==Inconsistent behavior on java#39;s ==(java的行为不一致==)
为什么 Java 能够将 0xff000000 存储为 int?Why is Java able to store 0xff000000 as an int?(为什么 Java 能够将 0xff000000 存储为 int?)