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?
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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 int j = 012 给出输出 10?
基础教程推荐
- RabbitMQ:消息保持“未确认"; 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- Struts2 URL 无法访问 2022-01-01
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
