java复合赋值运算符和赋值运算符
我在理解java中的复合赋值运算符和赋值运算符时遇到一些问题。有人可以向我解释一下这两个运算符真正是如何工作的吗? (在某个地方,我发现了一个非常好的示例代码,使用临时变量来解释工作原理,但遗憾的是我已经丢失了它。)非常感谢您的优势。这是我的小示例代码(我已经知道前缀和后缀运算符之间的区别):
int k = 12;
k += k++;
System.out.println(k); // 24 -- why not (12+12)++ == 25?
k = 12;
k += ++k;
System.out.println(k); // 25 -- why not (1+12)+(1+12) == 26?
k = 12;
k = k + k++;
System.out.println(k); // 24 -- why not 25? (12+12)++?
k = 12;
k = k++ + k;
System.out.println(k); // 25 -- why not 24 like the previous one?
k = 12;
k = k + ++k;
System.out.println(k); // 25 -- OK 12+(1+12)
k = 12;
k = ++k + k;
System.out.println(k); // 26 -- why?
I have some problem understanding the compound assignment operator and the assignment operator in java. Can someone explain to me how these two operators really works? (Somwhere I found a really good example code using temporary variables to explain the working but sadly I've lost it.) Thank you very much in advantage. Here is my little example code for them (I already know the difference between prefix and postfix operators):
int k = 12;
k += k++;
System.out.println(k); // 24 -- why not (12+12)++ == 25?
k = 12;
k += ++k;
System.out.println(k); // 25 -- why not (1+12)+(1+12) == 26?
k = 12;
k = k + k++;
System.out.println(k); // 24 -- why not 25? (12+12)++?
k = 12;
k = k++ + k;
System.out.println(k); // 25 -- why not 24 like the previous one?
k = 12;
k = k + ++k;
System.out.println(k); // 25 -- OK 12+(1+12)
k = 12;
k = ++k + k;
System.out.println(k); // 26 -- why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,在所有情况下,对 k 的赋值都会覆盖右侧可能发生的任何增量。
将注释内嵌:
k++
表示在使用该值之后递增,因此这与编码k = 12 + 12
相同++k
表示在使用该值之前递增,因此这与编码k = 12 + 13
k++ 表示之后递增您已经使用了该值,因此这与编码
k = 12 + 12
相同k++
表示在您使用该值之后递增 ,所以这与编码k = 12 + 13
相同++k
表示在使用该值之前递增,所以这是相同的作为编码k = 12 + 13
++k
表示在使用该值之前递增,然后再次使用该值,因此这与编码k = 13 + 相同13
Note that in all cases, the assignment to k overwrites any incrementation that may happen on the righthand side.
Putting comments in-line:
k++
means increment after you've used the value, so this is the same as codingk = 12 + 12
++k
means increment before you use the value, so this is the same as codingk = 12 + 13
k++
means increment after you've used the value, so this is the same as codingk = 12 + 12
k++
means increment after you've used the value, so this is the same as codingk = 12 + 13
++k
means increment before you use the value, so this is the same as codingk = 12 + 13
++k
means increment before you use the value, which is then used again, so this is the same as codingk = 13 + 13
下面是第一种情况的详细解释:
int k = 12;
k+=k++;
System.out.println(k);
k += k++;
相当于:k = k + (k++);
k + (k++);
从左到右计算。第一个 k 的值为 12。
k++
被计算为 k 的原始值(即 12); k 随后递增。其他帖子很好地解释了其他案例。
但这里有一个有趣的例子,它显示了从左到右的求值和右侧的后增量:
int k = 12;
k = k + k++ + k;
System.out.println(k);
k + (k++) + k;
从左到右计算。第一个 k 的值为 12。
第二个 k:
k++
被计算为 k 的原始值(即 12); k 随后递增。第三个 k:现在 k 的增量值为 13(因为它出现在第二个 k 之后)。
总结果是 37(即 12 + 12 + 13)。
Here is a detailed explanation for the first case:
int k = 12;
k += k++;
System.out.println(k);
k += k++;
is equivalent to:k = k + (k++);
k + (k++);
is evaluated from left to right.The first k has a value of 12.
k++
is evaluated to the original value of k (i.e. 12); k is later incremented.The other posts do a good job explaining the other cases.
But here is an interesting case that shows the evaluation from left to right and the post incrementation on the right:
int k = 12;
k = k + k++ + k;
System.out.println(k);
k + (k++) + k;
is evaluated from left to right.The first k has a value of 12.
Second k:
k++
is evaluated to the original value of k (i.e. 12); k is later incremented.Third k: now k has an incremented value of 13 (since it comes after the second k).
The total result is 37 (i.e. 12 + 12 + 13).
从这里完成一点:
http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
让我们仔细看看“i = i++;”这行代码的含义。执行:
评估“i++”。 “i++”的值是增量发生之前i的值。
作为“i++”评估的一部分,i 加一。现在i的值为1;
分配已执行。 i 被赋予“i++”的值,即 i 递增之前的值 - 即 0。
也就是说,“i = i++”大致翻译为
换句话说,增量最后发生是一种常见的误解。当表达式被计算时,增量会立即执行,并且增量之前的值会被记住以供将来在同一语句中使用。
A little completion from here:
http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
Let's take a close look at what the line "i = i++;" does:
"i++" is evaluated. The value of "i++" is the value of i before the increment happens.
As part of the evaluation of "i++", i is incremented by one. Now i has the value of 1;
The assignment is executed. i is assigned the value of "i++", which is the value of i before the increment - that is, 0.
That is, "i = i++" roughly translates to
With other words, it is a common misconception that the increment is happening last. The increment is executed immediately when the expression gets evaluated, and the value before the increment is remembered for future use inside the same statement.