example:
short n = 10;
n = n + 1;//编译失败
n += 1;
the difference:
n += 1; will not change the data type of the variable itself.
And n = n + 1; compilation fails, because 1 is int by default, and the result of the operation n becomes int type.
Extension exercises:
Code:
int i = 1;
i *= 0.1;
System.out.println(i);
i ++;
System.out.println(i);
Output result:
0
1
Reason: 1 * 0.1=0.1; but *= does not change the data type of the variable, float is converted to int, so 0.1 becomes 0, and then execute i++, the result is 1.
Combined training:
Code:
int m = 2;
int n = 3;
n *= m++;
System.out.println("m=" + m);
System.out.println("n=" + n);
Output:
m=3
n=6