java 为什么 09 是一个“太大”的整数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6935345/
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 is 09 "too large" of an integer number?
提问by Mohammad Faisal
They think it is:
他们认为是:
Possible Duplicate:
Integer with leading zeroes
可能的重复:
带前导零的整数
But if you check Integer with leading zeroesthen you will find that the question is asked if before the launch of jdk7 and therefore it has lower researching efforts. But in jdk7 there is some change and addition to the integers. Here are the answers which are up to date covering jdk7.
但是如果你用前导零检查Integer,那么你会发现这个问题是在jdk7发布之前提出的,因此它的研究工作量较低。但是在 jdk7 中,整数有一些变化和增加。以下是有关 jdk7 的最新答案。
I've a code:
我有一个代码:
class Test{
public static void main(String[] args){
int x=09;
System.out.println(x);
}
}
On compilation it gives an error: integer number too large : 09
在编译时它给出一个错误:整数太大:09
Why it do so?
为什么这样做?
Again, if I change the code to:
同样,如果我将代码更改为:
class Test{
public static void main(String[] args){
int x=012;
System.out.println(x);
}
}
Now the output is 10
现在输出是 10
Why it give the output 10 instead of 12?
为什么它给输出 10 而不是 12?
回答by miku
Numbers beginning with 0
are considered octal– and 9 is not an octal digit (but (conventionally) 0-7 are).
以 开头的数字0
被认为是八进制- 9 不是八进制数字(但(通常)0-7 是)。
Hexadecimal literals begin with 0x
, e.g. 0xA
.
十六进制文字以 开头0x
,例如0xA
。
Up until Java 6, there was no literal notation for binary and you'll had to use something like
直到 Java 6,二进制没有文字符号,你必须使用类似的东西
int a = Integer.parseInt("1011011", 2);
where the second argument specifies the desired base.
其中第二个参数指定所需的基数。
Java 7 now has binary literals.
Java 7 现在有二进制文字。
In Java SE 7, the integral types (byte, short, int, and long) can also be expressed using the binary number system. To specify a binary literal, add the prefix
0b
or0B
to the number.
在 Java SE 7 中,整数类型(byte、short、int 和 long)也可以使用二进制数系统表示。要指定二进制文字,请将前缀
0b
或添加0B
到数字。
回答by JimN
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
以“0”开头的整数文字被视为八进制。允许的数字为 0 到 7。
回答by Ernest Friedman-Hill
Integers beginning with the digit 0 are octal(base 8) numbers. The largest octal digit is 7; after 07 comes 010 (which is equal to decimal 8!)
以数字 0 开头的整数是八进制(基数为 8)的数字。最大的八进制数为 7;07之后是010(等于十进制8!)
012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.
012(八进制十二)就是010(八进制十,也就是十进制的8)加2,或者十进制的10。
回答by n0rm1e
09 is an octal numeric literal, an invalid one though.
09 是八进制数字文字,但无效。
Hexadecimal numbers start with 0x, like 0xFFFF.
十六进制数以 0x 开头,例如 0xFFFF。
There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.
Java 中过去没有二进制文字。Java 7 支持它们,从 0b 开始,比如 0b00100001。
回答by kliu
Numbers beginning with 0 are octal number. http://en.wikipedia.org/wiki/Octal
以 0 开头的数字是八进制数。 http://en.wikipedia.org/wiki/Octal