Hi Gururaj,
It did not work with casting also so had 2 modify the code a little
public class OverFlow
{
public static void main(
String[] args)
{
int i = 2147483647 + 1;
int j = 127+1;
byte b = (byte) j;
System.out.println(i+" "+j+" "+b);
}
}
results in
-2147483648 128 -128
Tool completed successfully
as expected but byte b = (byte)127+1 gives compiler error.
Originally posted by Shyamsundar Gururaj:
My best guess is ...
In the expression [b]byte b = 127 + 1; , the right hand side (source) gets upgraded to an int and since the destination is a byte and there is no explicit casting, the compiler flags an error. Try byte b = (byte) (127 + 1); ... It'll work fine.
The second expression is pretty obvious. The source does not fit into the destination.
Coming to the third, expressions are not evaluated at compile time. The compiler just checks whether the source can fit into the destination. It does so in this case and the compiler is satisfied. When the expression is actually evaluated, the integer cup overflows and consequently, the odometer is set to the maximum negative integer, -2147483648.
Hope this helps
Shyam
[This message has been edited by Shyamsundar Gururaj (edited September 13, 2001).][/B]