简体   繁体   English

java复合赋值运算符和赋值运算符

[英]java compound assignment operator and assignment operator

I have some problem understanding the compound assignment operator and the assignment operator in java. 我在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. (Somwhere我发现了一个非常好的示例代码,使用临时变量来解释工作,但遗憾的是我已经失去了它。)非常感谢你的优势。 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?

Note that in all cases, the assignment to k overwrites any incrementation that may happen on the righthand side. 请注意,在所有情况下,对k的赋值都会覆盖右侧可能发生的任何增量。

Putting comments in-line: 将评论放在一行:

   int k = 12;
   k += k++;   
   System.out.println(k);  // 24

k++ means increment after you've used the value, so this is the same as coding k = 12 + 12 k++表示在使用该值递增,因此这与编码k = 12 + 12

   k = 12;
   k += ++k; 
   System.out.println(k); // 25

++k means increment before you use the value, so this is the same as coding k = 12 + 13 ++k表示使用该值之前递增,因此这与编码k = 12 + 13

   k = 12;
   k = k + k++; 
   System.out.println(k); // 24

k++ means increment after you've used the value, so this is the same as coding k = 12 + 12 k++表示在使用该值递增,因此这与编码k = 12 + 12

   k = 12;
   k = k++ + k; 
   System.out.println(k); // 25

k++ means increment after you've used the value, so this is the same as coding k = 12 + 13 k++表示在使用该值递增,因此这与编码k = 12 + 13

   k = 12;
   k = k + ++k; 
   System.out.println(k); // 25

++k means increment before you use the value, so this is the same as coding k = 12 + 13 ++k表示使用该值之前递增,因此这与编码k = 12 + 13

   k = 12;
   k = ++k + k; 
   System.out.println(k); // 26

++k means increment before you use the value, which is then used again, so this is the same as coding k = 13 + 13 ++k表示使用该值之前递增,然后再次使用该值,因此这与编码k = 13 + 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 + (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 (ie 12); 第一个k的值为12. k++被评估为k的原始值(即12); k is later incremented. k后来递增。

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. 第一个k的值为12。
Second k: k++ is evaluated to the original value of k (ie 12); 第二个k: k++被评估为k++的原始值(即12); k is later incremented. k后来递增。
Third k: now k has an incremented value of 13 (since it comes after the second k). 第三个k:现在k的增量值为13(因为它在第二个k之后)。
The total result is 37 (ie 12 + 12 + 13). 总结果为37(即12 + 12 + 13)。

A little completion from here: http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment 从这里稍作完成: http//www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment

Let's take a close look at what the line "i = i++;" 让我们仔细看看“i = i ++”这一行是什么。 does: 作用:

"i++" is evaluated. 评估“i ++”。 The value of "i++" is the value of i before the increment happens. “i ++”的值是增量发生之前的i值。 As part of the evaluation of "i++", i is incremented by one. 作为评估“i ++”的一部分,i增加1。 Now i has the value of 1; 现在我的值为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 我被赋予“i ++”的值,这是增量前的i的值 - 即0。即,“i = i ++”粗略地转换为

int oldValue = i; 
i = i + 1;
i = oldValue; 

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. 在计算表达式时立即执行增量,并记住增量之前的值以供将来在同一语句中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM