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?
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.
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.
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
or0B
to the number.
Integer literals beginning with a "0" are treated as octal. The permissible digits are 0 through 7.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With