Hi,
When assigning an int value to a byte variable, my understanding is that we have to case explicitly as:
int i=9;
byte b= (int)i;
but when i run following piece of code(with modifier FINAL before int declaration and assignment it works fine without throwing any cast exceptions:
class Test {
public static void main(String args[])
{
final int i = 12;
byte b = i;
System.out.println(b);
}
}
Why is it so? I expected the code to throw caste exception.
When assigning an int value to a byte variable, my understanding is that we have to case explicitly as:
int i=9;
byte b= (int)i;
but when i run following piece of code(with modifier FINAL before int declaration and assignment it works fine without throwing any cast exceptions:
class Test {
public static void main(String args[])
{
final int i = 12;
byte b = i;
System.out.println(b);
}
}
Why is it so? I expected the code to throw caste exception.