This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Sherry, please give your topic a meaningful title, and please tell us the compilation error you get. Is the error given by the compiler not meaningful?
Yes, I can see why the second does not compile:In the first case i is a compile time constant within the range of a byte (-128 to 127). In the second it is not a constant and the compiler cannot guarantee in all possible cases that at the time of the assignment that i still contains 100. [ September 06, 2005: Message edited by: Barry Gaunt ]
Reason is simple in the first code u declared variable as a final and final variables are constants so compiler knows that the value of this particular variable will not change in runtime ie. will not change from 100. So it will not go out of range.
In other way compiler replaces all final variable with there values at compile time as in ur code
statement byte b = i; //for first code
will replace to
byte b = 100; before compilation then compilation will occur.