Java 为什么带有前导零的整数文字会被奇怪地解释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/565634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are integer literals with leading zeroes interpreted strangely?
提问by IAdapter
This prints 83
这打印了 83
System.out.println(0123)
However this prints 123
但是这会打印 123
System.out.println(123)
Why does it work that way?
为什么它会这样工作?
采纳答案by luiscubal
A leading zero denotes that the literal is expressed using octal (a base-8 number).
前导零表示文字使用八进制(基数为 8 的数字)表示。
0123 can be converted by doing (1 * 8 * 8) + (2 * 8) + (3)
, which equals 83 in decimal.
For some reason, octal floats are not available.
0123 可以通过做 来转换(1 * 8 * 8) + (2 * 8) + (3)
,它等于十进制的 83。由于某种原因,八进制浮点数不可用。
Just don't use the leading zero if you don't intend the literal to be expressed in octal.
如果您不打算用八进制表示文字,请不要使用前导零。
There is also a 0x
prefix which denotes that the literal is expressed in hexadecimal (base 16).
还有一个0x
前缀表示文字以十六进制(基数为 16)表示。
回答by toolkit
回答by ryeguy
Try this:
尝试这个:
public static String leftPad(int n, int padding) {
return String.format("%0" + padding + "d", n);
}
leftPad(5, 3); // return "005"
leftPad(15, 5); // return "00015"
leftPad(224, 3); // return "224"
leftPad(0, 4); // return "0000"
回答by neoms21
first one printed as 83 because java takes 0123 as octal number and it prints decimal equivalent of that number.
第一个打印为 83,因为 java 将 0123 作为八进制数,并打印该数字的十进制等效值。
回答by TofuBeer
printf
will do it: http://java.sun.com/developer/technicalArticles/Programming/sprintf/
printf
会这样做:http: //java.sun.com/developer/technicalArticles/Programming/sprintf/
public class X
{
public static void main(final String[] argv)
{
System.out.printf("%04d", 123);
System.out.println();
}
}
You could also make it "%0" + size + "%d"
if you wanted to vary the length... though if the lengths were common I'd probably make constants like "%04d"
, "%012d"
, etc...
"%0" + size + "%d"
如果您想改变长度,您也可以制作它……尽管如果长度很常见,我可能会制作诸如"%04d"
, 之类的常量"%012d"
……
回答by Peter Lawrey
The octal (leading 0) and hexadecimal (leading 0x) were inherited from C. For comparison, try
八进制(前导 0)和十六进制(前导 0x)继承自 C。为了比较,请尝试
System.out.println(0x123);
回答by Anil Satija
In Java integer literals with a leading zero are octal integers (base 8).
在 Java 中,带有前导零的整数文字是八进制整数(基数为 8)。
(1 * 8^2) + (2 * 8^1) + (3 * 8^0) = 83
So do not use any number leading with 0 if you don't want to treat it as an octal number.
因此,如果您不想将其视为八进制数,请不要使用任何以 0 开头的数字。
回答by Devendra Lattu
0123 -> 83
1010L -> 1010
0101L -> 65
The numbers 1010L
and 0101L
are not in binary representation (just to avoid the confusion).
These numbers are in decimal representation.
数字1010L
和0101L
不是二进制表示(只是为了避免混淆)。
这些数字以十进制表示。
Even as per the Regex patterns in Oracle docs,
即使按照Oracle 文档中的 Regex 模式,
\0n is the character with octal value 0n (0 <= n <= 7)
\xhh is the character with hexadecimal value 0xhh
\0n 是八进制值为 0n 的字符 (0 <= n <= 7)
\xhh 是十六进制值为 0xhh 的字符
Thus, your number 0101
be it in Integer
or Long
format is treated as an Octal representation of a number.
因此,您的数字0101
无论是格式Integer
还是Long
格式都被视为数字的八进制表示。
123 => 1 * 8^2 + 2 * 8^1 + 1 * 8^0 = 83
0101 => 1 * 8^2 + 0 * 8^1 + 1 * 8^0 = 64 + 0 + 1 = 65