Originally posted by Leandro Melo:
The following is more interesting. It doesn't compile for the first 3 cases, but it compiles for the last one.
Block 2:
It'd have been very easy to guess that the fourth option wouldn't compile, but it actually does.
The point is that in Block 1 initializations the compile error for the first 3 is a casting error (cannot convert from int to short, char, etc...). But the compile error for the last case in Block 1 is an out of range error.
Then I guess that in block 2, the compiler is not smart enoght to realize that it adding one to the int limit will became out of range, am I right???
Well, the only explanation I see is compiler doesn't perform value checking, only type checking. The result of + operator on numbers is always typed at least int, therefore first 3 lines cannot compile without the casts. Fourth line is typed int, so it compiles. Of course the 'smart' compiler could check the value (I'm not sure what JLS says about it), but what in case of some other compiler that does not do it?
Adam
SCJA, SCJP 5.0, SCWCD 1.4, SCBCD 5, SCEA 5