I couldn't make difference between the two snippet's below
The first one compiles out, while other runs fine. The only diff is using the final key in the second. This is from dan's questions and he does not mention anything on the final key.
Joined: Jul 02, 2002
Thank you for using my exam.
The first code fragment is as follows.
char c1 = 1; byte a = c1;
In the first case, the code attempts to assign a char value to a byte variable. The char is a 16 bit unsigned type while byte is an 8 bit signed type. As you can imagine, there are a lot of char values that can not be represented by a variable of type byte.
The second code fragment is as follows.
final char c1 = 1; byte a = c1;
In the second case, the "final" modifier is added to the declaration of the char variable. As a result, the char variable is now a compile-time constant. The compiler recognizes that the constant value, 1, can be assigned to a variable of type byte.
This is covered in Section 5.2 of the Java Language Specification.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>