short s1 = 1; s1 = s1 + 1; 和 short s1 = 1; s1 += 1;

short s1 = 1; s1 = s1 + 1;编译不能通过

short s1 = 1; s1 += 1;编译可以通过

因为:

对于short s1 = 1; s1 = s1 + 1;由于1是int类型,因此s1+1运算结果也是int 型,需要强制转换类型才能赋值给short型。

而short s1 = 1; s1 += 1;可以正确编译,因为s1+= 1;相当于s1 = (short)(s1 + 1);其中有隐含的强制类型转换。

猜你喜欢

转载自blog.csdn.net/qq_38006520/article/details/82527619
今日推荐