为什么int j = 012给出输出10? [英] Why int j = 012 giving output 10?
本文介绍了为什么int j = 012给出输出10?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的实际项目中这是偶然发生的,这是我修改过的小程序。
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?
Than I change 012 to 0123 and now it is giving output 83?
任何人都可以解释原因吗?
Can anyone explain why ?
推荐答案
比我改为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 ^ 3 = 83
这篇关于为什么int j = 012给出输出10?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文