- /*
- 赋值运算符:
- 1.=: 从右至左;
- 2.+=;
- 3.-=:
- 4.*=;
- 5./=;
- 6.%=;
- */
- class Demo
- {
- public static void main(String[] args)
- {
- //1.=
- int a = 10;//a = 10
- int b = a;// b = 10
- int c = a + b;//c = 20
- a = a + b;//a = 10 + 10
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- System.out.println("c = " + c);
- //注意一个问题:
- int v;
- v += 20;//v = v + 20;编译错误,v此时没有值;
-
- //2.+=,-=,*=,/=,%=这些符号都带"强制转换功能"
- int n = 20;
- int m = 30;
- n += 30;//n = (int)(n + 30)//n = 50
-
- System.out.println("n = " + n);
- byte b1 = 10;
- // b1 = b1 + 20;//编译错误
- b1 += 20;//b1 =(byte)(b1 + 20);
- //3.*=
- int x = 10;
- x *= 20 - 10;//x = x * (20 - 10)
- System.out.println("x = " + x);//100
-
- }
- }
复制代码
|
|