I am preparing to take the SCJP 6.0.
I am primarily using Sierra and Bates for my preparation.
I found some interesting (read those that I could not answer
questions on boxing, and would welcome any help to understand this topic a little better.
1. Long and Integer extend Number.
***Why does 12 get boxed to an Integer object, when I am only assigning it to a Supertype Number? I mean, why did it not get Boxed to Number directly?***
Number x = 12;
May be the reason I cannot cast an Integer x to Long is because they are not on the same class tree..i.e, Long does not extend Integer, and Integer does not extend Long. Please clarify if my assumption is right.
Number y = (Long) x;
2. I can do this
int x = 1;
Number y = x;
but when I do this..
if (y == x) I get an error..
would it be safe for me to assume that, a Number object and a Integer object cannot be compared, even if Integer is ONLY a subclass of Number. I ask this question becuase the compiler does not complain when I try to do this.
int x = 1;
Integer y = x;
if (y == x)..no errors