Java - short s = 1; s = s + 1; 有错吗? short s = 1; s += 1; 有错吗?

版权声明:欢迎转载并请注明出处,谢谢~~ https://blog.csdn.net/chimomo/article/details/78342155

对于 short s = 1; s = s + 1; 由于1是int类型,因此 s + 1 运算结果也是int 型,需要强制转换类型才能赋值给short型。而 short s = 1; s += 1; 可以正确编译,因为 s += 1; 相当于 s = (short)(s + 1); 其中有隐含的强制类型转换。

猜你喜欢

转载自blog.csdn.net/chimomo/article/details/78342155