问题描述
为什么数字从字节变成整数,或者从短到字节变成整数?
代码如下:
public class TypeCasting {
public static void main ( String [] args ) {
short c = 292;
int d = (byte) c;
System.out.println(d); // 36
}
}
解决方法
292 = 0001 0010 0100
36 = 0000 0010 0100
将 292 从 Short 更改为 Byte 会删除左侧位(前 4 位)。当您将其重新转换为 Int 时,这些位现在丢失了,因此您仍然只剩下 36。