表达式之谜---半斤(复合赋值表达式)

xiaoxiao2022-06-12  40

简单赋值操作符 =

复合赋值操作符 += , -=, *= ,/= , %= ,<<=,>>=,>>>=, &=,等

 

1. 复合赋值表达式自动将所执行计算的结果转型为其左侧变量类型。复合表达式会悄悄产生一个转型。

2. 不要将复合赋值操作符作用与byte,short,char类型变量。同样int型数的右边要确保不是long、float、double类型

 

代码:

public class Banjin { /** * 半斤 */ public static void main(String[] args) { short x = 0; int y = 123456; System.out.println(x+=y);//产生转型,将int // 提示Type mismatch: cannot convert from int to short //System.out.println(x=x+y); } }

 

转载请注明原文地址: https://www.6miu.com/read-4933557.html

最新回复(0)