i =i++ + i++即i++的个人理解

i++

i++的含义用伪代码表示为

	public Integer addadd(Integer i) {
		Integer t = i;
		i = i+1;
		return t;
	}

++i

++i的含义用为代码表示为

	public Integer addadd(Integer i) {
		i = i+1;
		return i;
	}

测试代码

	public static void main(String[] args) {
		int i = 0;
		i = i++;
		System.out.println(i);
		i = i++ + i++;
		System.out.println(i);
		i =  0;
		i = i++ + i++ + i++;
		System.out.println(i);
	}

上面代码输出为
0
1
3

以下代码的含义

i = i++ + i++ + i++;
i++的运算级别高于+,-,*,/ 所以先运算i++,
第一个i++ 返回为0,但i的值已经为1;
第二个i++,i值运算前为1,所以返回为1,i值运算后为2;
第三个i++,i值运算前为2,所以返回为2,i值运算后为3;
所以以上就等于 i=0+1+2=3

猜你喜欢

转载自blog.csdn.net/m0_38012501/article/details/86135148
i++