0
Casting of integer to byte
when I ran the following code - int a=130; byte b= (byte) a; System. out. println(b); //Output. -126. Can anyone explain how?
2 Answers
+ 3
It seems that the byte type has a range of -128 to 127, as it is signed. So when you assign a number 130 to a byte variable, the variable undergoes overflow and for 128 the output moves to the highest negative number, -128. So when the input is 130, the number overflows to -126 and thus you get the output -126.
+ 1
Thanks Kinshuk....