i++先给出i的值再++。
++i先计算++再给出i的值
int i = 0, j = 0, k = 0, l = 0;
i = i++ + i++;
j = ++j + ++j;
k = k++ + ++k;
l = ++l + l++;
System.out.println(i + " " + j + " " + k + " " + l);
结果是:
1 3 2 2
原因是:
int a = 0;
a = a++;
System.out.println(a)
结果是0
java采用了中间缓存机制,a=a++可以写成
temp = a;
a =a+1;
a = temp;