This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details

Debug note < Add assignment + = behavior in expression >

Ask questions

Recently I ran into a problem: understanding the chain of assignment operators.

In answer to this question,

I began to doubt my own understanding of the behavior of the addition assignment operator += or any other operator= (&=, *=, /=, etc.).

My question is, what is the logic behind when the variables in the following expression are updated in place so that their changed values during evaluation are reflected elsewhere in the expression?

Look at the following two expressions:

The expression 1

a = 1
b = (a += (a += a))
//b = 3 is the result, but if a were updated in place then it should've been 4
Copy the code

The expression of 2

a = 1
b = (a += a) + (a += a)
//b = 6 is the result, but if a is not updated in place then it should've been 4
Copy the code

In the first expression, when the innermost expression is evaluated (a += a), it appears that the value a is not updated, so the result is 3 instead of 4.

However, in the second expression, the value a of is updated, so the result is 6.

When should we assume that the value of A will be reflected elsewhere in the expression, and when should we not?

Answer a:

Remember that a += x really means a = a + x. The key thing to understand is that addition evaluates from left to right,

So let’s figure out what b = (a += (a += a)) is. First, we use the rule a += x means a = a + x, and then start carefully evaluating the expressions in the correct order:

  • b = (a = a + (a = a + a))becausea += xmeansa = a + x
  • b = (a = 1 + (a = a + a))becauseaIs the current1. So let’s compute the left term first,aLet’s do the right term(a = a + a)
  • b = (a = 1 + (a = 1 + a))becauseaor1
  • b = (a = 1 + (a = 1 + 1))becauseaor1
  • b = (a = 1 + (a = 2))because1 + 1is2
  • b = (a = 1 + 2)becauseanow2
  • b = (a = 3)because1 + 2is3
  • b = 3becauseanow3

This leads us to a = 3 and b = 3 as explained above. Let’s try using other expressions b = (a += a) + (a += a) :

  • b = (a = a + a) + (a = a + a)
  • b = (a = 1 + 1) + (a = a + a)Remember, we calculate the left term first and then the right term
  • b = (a = 2) + (a = a + a)
  • b = 2 + (a = a + a)And a is now 2. Let’s do the correct arithmetic
  • b = 2 + (a = 2 + 2)
  • b = 2 + (a = 4)
  • b = 2 + 4andaFor now4
  • b = 6

That leaves us with a = 4 and b = 6. This can be verified by printing a and B using Java/JavaScript (both have the same behavior here).

Answer two:

Here are the rules to be aware of

  • Operator priority
  • Variable assignment
  • Express calculation

The expression 1

a = 1
b = (a += (a += a))

b = (1 += (a += a))  // a = 1
b = (1+ = (1 += a))  // a = 1
b = (1+ = (1+ =1))  // a = 1
b = (1+ = (2))  // a = 2 (here assignment is -> a = 1 + 1)
b = (3)  // a = 3 (here assignment is -> a = 1 + 2)
Copy the code

The expression of 2

a = 1
b = (a += a) + (a += a)

b = (1 += a) + (a += a) // a = 1
b = (1+ =1) + (a += a) // a = 1
b = (2) + (a += a) // a = 2 (here assignment is -> a = 1 + 1)
b = (2) + (2 += a) // a = 2 (here here a = 2)
b = (2) + (2+ =2) // a = 2
b = (2) + (4) // a = 4 (here assignment is -> a = 2 + 2)
b = 6 // a = 4
Copy the code

The expression of 3

a = 1
b = a += a += a += a += a

b = 1+ =1+ =1+ =1+ =1 // a = 1
b = 1+ =1+ =1+ =2 // a = 2 (here assignment is -> a = 1 + 1)
b = 1+ =1+ =3 // a = 3 (here assignment is -> a = 1 + 2)
b = 1+ =4 // a = 4 (here assignment is -> a = 1 + 3)
b = 5 // a = 5 (here assignment is -> a = 1 + 4)
Copy the code

The article translated from Stack Overflow: stackoverflow.com/questions/5…