3

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?
1
  • The basis of the logic is that ++k the increase is executed before the main operation and in k++ the main operation is executed first. Commented Sep 13, 2011 at 21:44

3 Answers 3

6

Note that in all cases, the assignment to k overwrites any incrementation that may happen on the righthand side.

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 = 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 = 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 = 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 = 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 = 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

Sign up to request clarification or add additional context in comments.

4 Comments

+1 - darn, beat me to it!
The 4th example, k = k++ + k should be equivalent to k = 12 + 13, right?
Sorry - you are right (I fixed it now). A bit too much copy-pasting going on :(
Thank you! The only question left is where is the ++ in the first example? I mean k = 12+12 is OK, but you have a k++ also, which comes after the assignment: k = 12+12; k = k+1; => k should be 25. Where is the error in my logic? Thx again!
1

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).

Comments

0

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.