This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
It is weird indeed. This is connected to the compiler I believe. If you change the first snippet of code to
final int i = 130; byte b = i; System.out.println(b);
the code generates the compilation error you would expect anyhow (because i>128). My assumption is this:
The compiler checks whether a loss of precision MAY take a place but i is final AND already asigned to 100 (in bit domain) therefore the compiler knows it can fit into a byte and this cannot change and I believe that for optimization purposes does not generate the error.
However if i>127 the error is generated as the conversion MUST take place and there is a big chance for the precision to be lost. However this trick is much too subtle.. I would have certainly overlooked it. You have a very good question .. I had to copy the code into Eclipse and test it to be shore that it doesn't throw a compilation error.
Better, faster, lighter Java ... you mean Ruby right ?
SCEA5,SCBCD1.3,SCWCD5,SCJP1.4 - memories from my youth.