-----------------<br />SCJP 1.4<br />Target SCWCD by December 2007
-----------------<br />SCJP 1.4<br />Target SCWCD by December 2007
byte b = 3; //No problem, 3 fits in a byte
byte c = 8; // No problem, 8 fits in a byte
byte d = b + c; // Should be no problem, sum of the two bytes
// fits in a byte
The last line won't compile! You'll get an error something like this:
TestBytes.java:5: possible loss of precision
found : int
required: byte
byte c = a + b;
^
We tried to assign the sum of two bytes to a byte variable, the result of which (11) was definitely small enough to fit into a byte, but the compiler didn't care. It knew the rule about int-or-smaller expressions always resulting in an int. It would have compiled if we'd done the explicit cast:
byte c = (byte) (a + b);
Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
-----------------<br />SCJP 1.4<br />Target SCWCD by December 2007
Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
Whereas at line 2, since the value is not known, there is a possibility that some value higher than what a byte could hold could be passed at runtime.
Yours Sam<br />SCJP5.0 97%<br />SCBCD5.0 72%
Don't get me started about those stupid light bulbs. |