关于整数值的前导零 [英] regarding leading zero in integer value
问题描述
我有以下代码
int a = 01111;
System.out.println("output1 = " + a);
System.out.println("output2 = " + Integer.toOctalString(1111));
输出
output1 = 585
output2 = 2127
我期待输出如下。
output1 = 2127
output2 = 2127
为什么我直接打印时会给出 585
int值?我期待java自动将值转换为前导零到八进制。
Why does it give 585
when I print direct int value ? I was expecting java to automatically convert value with leading zero to octal.
01111
和 585
之间的关系是什么?
What is the relation between 01111
and 585
?
推荐答案
前导0表示八进制数字(基数为8)。
Leading 0 signifies an octal number (base 8).
01111(八进制)是1 * 8 ^ 3 + 1 * 8 ^ 2 + 1 * 8 ^ 1 + 1 * 8 ^ 0 = 585(十进制)
01111 (octal) is 1*8^3+1*8^2+1*8^1+1*8^0=585 (decimal)
Integer.toOctalString(1111)
将十进制数1111转换为八进制字符串。 2127八进制(2 * 8 ^ 3 + 1 * 8 ^ 2 + 2 * 8 ^ 1 + 7 * 8 ^ 0)是1111十进制。
Integer.toOctalString(1111)
converts the decimal number 1111 to an octal String. 2127 octal (2*8^3+1*8^2+2*8^1+7*8^0) is 1111 decimal.
这篇关于关于整数值的前导零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!