short a = 5;
short b = 8;
short result = a + b;
System.out.println(result);
编译会出错
short/byte/char
等数据类型在进行运算时会自动转换为int类型,所以输出为short会出错
short result = 5 + 8;
相当于:
short result = 13;
在编译器编译的阶段就进行了计算,这就是编译器的优化
byte a = 30;
如果右边的值不超过byte的范围
编译器内部自动转换成
byte a = (byte) 30;