Originally posted by Tresa P Anthony:
Hi,
Could you please explain the following?
On compilation, i get a compile time error only at line 1 but not at line 2 as i had expected. Please explain why compiler okays line 2.
I am using jdk 1.4.
Thanks
[ August 15, 2006: Message edited by: Tresa P Anthony ]
The reason for the compile-time error in line 1 is that in a binary addition operation involving int and byte,char, or short, the other operand is promoted to an int, and the result of the operation is of type int. You cannot assign an int which is not a compile-time constant in the range of byte into a byte without an explicit cast.
In the second line, it only matters that the type of the expression is int.
The compiler does not carry out any calculations. It is only checking type in this case.