int einInt;
byte einByte;
einInt = 160;
einByte = (byte) einInt;
System.out.println(einByte);
// System.out.println(einByte); now outputs -96
// please try to explain this to me as detailed as possible
CodePudding user response:
A byte in Java is an 8-bit signed integer, meaning it has a range of -128...127. 160 falls outside of that range, so it overflows back to the beginning of that range, and it overflows by 33 (160 - 127 = 33). Adding 1 to a byte value of 127 will overflow to -128, but since the overflow was 33, it then adds 32 to -128 which results in -96.