Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

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 integers. 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?

like image 725
Mohammad Faisal Avatar asked Aug 04 '11 02:08

Mohammad Faisal


People also ask

Is 09 integer in Java?

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.

How do I fix too large integers in Java?

Fix: If you are dealing with numbers in your Java program and it gets too long! (over 2,147,483,647 to be precise) then it a type long and not int so you need to suffix the letter capital L or lower case l to fix the compilation error.


2 Answers

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.

like image 115
miku Avatar answered Sep 20 '22 17:09

miku


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

like image 44
JimN Avatar answered Sep 22 '22 17:09

JimN