无法在java中完成此任务 [英] unable to make out this assignment in java

查看:81
本文介绍了无法在java中完成此任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以解释为什么会发生这种情况

can anybody explain this why its happening

int i=0;
i=i++;
i=i++;
i=i++;
System.out.println(i);

它打印零。

推荐答案

i ++ 是一个后增量( JLS 15.14.2 )。它增加 i ,但表达式的结果是 i 之前的值增量。将此值重新分配回 i 实际上保持 i 的值不变。

i++ is a postincrement (JLS 15.14.2). It increments i, but the result of the expression is the value of i before the increment. Assigning this value back to i in effect keeps the value of i unchanged.

将其分解为:

int i = 0;
int j = i++;

很容易理解为什么 j == 0 在这种情况下。现在,我们用 i 代替左边的 j 。右侧值仍为 0 ,这就是您在代码段中获得 i == 0 的原因。

It's easy to see why j == 0 in this case. Now, instead of j, we substitute the left hand side with i. The right hand side value is still 0, and that's why you get i == 0 in your snippet.

这篇关于无法在java中完成此任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆