如果我将整数 160 分配给整数变量并尝试将其存储在字节变量中,为什么值会从 160 变为 -96?

If I assign and integer variable the integer 160 and try to store it in a byte variable, why does the value change from 160 to -96?

int einInt;
byte einByte;

einInt = 160;
einByte = (byte) einInt;
System.out.println(einByte);

// System.out.println(einByte);现在输出 -96
// 请尽可能详细地向我解释一下

Java 中的一个字节是一个 8 位有符号整数,这意味着它的范围是 -128...127。 160 落在该范围之外,因此它溢出回到该范围的开头,并且溢出 33 (160 - 127 = 33)。将 1 加到字节值 127 将溢出到 -128,但由于溢出是 33,它随后将 32 加到 -128,结果为 -96。