You're using Java >= 1.5, right? The following line also works and gives you a hint as to what's going on...
In the original code, the 0 was boxed into an Integer, and type of the conditional expression is Integer, then the result of that was unboxed so it could be assigned to an int. Of course, when you unbox null, the result is a NPE.
Here, I think the compiler is being too accomodating with boxing/unboxing
Originally posted by Santosh Deshpande: Thanks to both of you Jeff and Ernest.. and yes I am using j2se 5.0. But why can't this will compile:
int i = null;
How can the compiler catch this one while ignoring the possible disaster in the previous one?
The difference is that the above is *sure* disaster, whereas the former code "only" was possible disaster...
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus