I have been testing the char casting and I went through this:
public class Test {
public static void main(String a[]) {
final byte b1 = 1;
byte b2 = 1;
char c = 2;
c = b1; // 1- Working fine
c = b2; // 2 -Compilation error
}
}
Can anyone explain why it's working fine in 1 when I added a final to the byte?
When the variable is
final, the compiler automatically inlines its value which is 1. This value is representable as achar, i.e.:is equivalent to
In fact, according to this section on
finalvariables,b1is treated as a constant: