Wednesday, March 20, 2013

Behavior of primitive type int with 0


We all use int as a primitive type in java. Like

int abc = 7; // which is perfectly fine

Lets say we declare,

int abc =007; // this is also fine

but if we declare

int abc = 008; or int abc = 009; ...--- Compilation error. Why ?

Because when we append zero before integer , compiler reads it as an Octal (base 8) literal.


What will be the output of following statement ?

1) System.out.print("Hi" + 007 ); 

2) System.out.print("Hi" + 008);

3) System.out.print("Hi" + 009 ); 


4) System.out.print("Hi" + 010);


Answers:


1) Hi7
2) Compilation error
3) Compilation error
4) Hi8







No comments:

Post a Comment