"like_java"-
two things...
1st thing:
Welcome to the JavaRanch! Please adjust your displayed name to match the
JavaRanch Naming Policy.
You can change it
here.
2nd thing:
Originally posted by like_java:
case 1: without final on line 5
Will give compilation error at line 7,
explicit cast needed for int to byte type
case 2: with final added before int on line 5
compiles, with o/p 100
final int i = 100;
byte b = i;
when i is marked as final, the compiler is sure of its value -- it knows without a doubt that the value 100 is small enough to be held within a byte -- so it knows there will be no loss of precision, and it'll let you do it.
however, if i is NOT marked as final, the compiler can't be sure of its value, and can't be sure that the value held in i is small enough to go into a byte without losing any precision.
So... just to get itself off the hook -- it makes you take the heat if there's a problem (loss of precision) and you have to explicitly cast it. As
Kathy and Bert write in their new certification book:
"A cast is nothing more than your way of saying to the comipler. 'Trust me. I'm a professional. I take full responsibility for anything weird that happens when [you convert that int to a byte].' "
...again welcome to the JavaRanch!

[ January 05, 2003: Message edited by: Jessica Sant ]