开发者

Why is 09 "too large" of an integer number? [duplicate]

This question already has answers here: Closed 11 years ago.

They think it is:

Possible Duplicate:

Integer with leading zeroes

But if you check Integer with leading zeroes then 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 integ开发者_如何学JAVAers. Here are the answers which are up to date covering 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

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

Why it give the output 10 instead of 12?


Numbers beginning with 0 are considered octal – and 9 is not an octal digit (but (conventionally) 0-7 are).


Hexadecimal literals begin with 0x, e.g. 0xA.


Up until Java 6, there was no literal notation for binary and you'll had to use something like

int a = Integer.parseInt("1011011", 2);

where the second argument specifies the desired base.


Java 7 now has binary literals.

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 or 0B to the number.


Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.


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!)

012 (octal twelve) is 010 (octal ten, which is decimal 8) plus 2, or decimal 10.


09 is an octal numeric literal, an invalid one though.

Hexadecimal numbers start with 0x, like 0xFFFF.

There used to be no binary literal in Java. Java 7 supports them, starting with 0b, like 0b00100001.


Numbers beginning with 0 are octal number. http://en.wikipedia.org/wiki/Octal

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜