Originally posted by Basanti Mathad:
Hi,
class E {
static byte a = (byte)127;
static byte b = (byte)128;
static byte c = (byte)255;
static byte d = (byte)256;
public static void main(String args[]) {
System.out.print(a + " " + b + " " + c + " " + d);
}
}
When int variable is casted to a byte var, only the lowest 8 bits are used. in your example,
127 is 0000,0000,0000,0000,0000,0000,0111,1111
cut the lowest 8 bits, the result is 0111,1111
128 is 0000,0000,0000,0000,0000,0000,1000,0000
do the same thing, the result is 1000,0000 (-128)
255 is 0000,0000,0000,0000,0000,0000,1111,1111
the result is 1111,1111 (-1)
256 is 0000,0000,0000,0000,0000,0001,0000,0000
the result is 0000,0000 (0)