Why does line 3 generate a compiler error and not line 5? I understand that byteW1.byteValue() + byteW2.byteValue(); returns an int value, and that passing a int to a byte without an explicit cast causes a compiler error; but if thats true, why does line 5 compile correctly?
Doesn't adding 4 + 4 return an int value also?
Thanks [ March 23, 2005: Message edited by: David Miranda ]
Because you're adding two constants, the math can be done at compile-time by the compiler, rather than being done while the program is running. Because the compiler can add 4+4 to get 8, and because it knows that 8 is a valid value for a byte, this line compiles just fine; the conversion from int to byte is (at least conceptually) done for you, automatically.
In line 3, the compiler isn't smart enough to know what byteValue() will return, so it can't be sure that the result of the addition -- which will be done at runtime -- is going to fit into a byte.