3

Doubtful result in the following code:

public static void main (String[] args)
{ 
int i = 2;
i = i+=2 + i++;
System.out.println(i); }

Was expecting 8 as output, as 'i+=2' should update i, but its not behaving so.

Output: 6

I infer that the short-hand assignment operator is returning 4 as expected but not updating the same in variable i. Any explanation will be appreciated.

Mat
  • 202,337
  • 40
  • 393
  • 406
CᴴᴀZ
  • 521
  • 7
  • 20

1 Answers1

8

i++ is a postfix increment - it increments i, then essentially returns the old value of i. The equivalent prefix operator, ++i, would return the "updated" value, but that's not what's being used here.

i+=2 works differently however, it's essentially equivalent to i+2, since it does return the updated value.

However, I think where the confusion arises is that you're looking at it like this:

i = (i += 2) + i++;

...which does give your expected result. i+=2 gives 4, and updates i to 4, then i++ returns 4 (instead of 5 since it's a post increment.) However, when you take operator precedence into the equation, Java actually "brackets" it like this by default:

i = i += (2 + i++);

Just to clear up any confusion, Java evaluates it this way because the += operator has least precedence in this example, and therefore the addition expression (+) is calculated first.

This bracketed statement is essentially equivalent to:

i = (i = i + (2 + i++));

Which in turn simplifies to:

i = i + (2 + i++);

So given the above statement, and evaluating from left to right, we first take the value of i (2), and then add to it the value of 2+i++; the latter giving 4 (because of the postfix increment). So our final result is 2+4, which is 6.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • And why is this returns 8: `i = 2; i = (i+=2) + i++;` ?? – Ahmed KRAIEM May 03 '13 at 13:46
  • Ahh, you changed your answer..!! I was about to point this flaw. – Ravi K Thapliyal May 03 '13 at 13:47
  • @AhmedKRAIEM Updated now to explain! – Michael Berry May 03 '13 at 13:51
  • @Ravi Yup, I realised what I'd written wasn't quite right as soon as I wrote it..! – Michael Berry May 03 '13 at 13:52
  • Subexpression are evaluated left to right, so the `i` to the left of `+=` is read before the increment. The increment actually does the last read of `i`. (Not that I can remember all the rules!) – Tom Hawtin - tackline May 03 '13 at 14:05
  • @berry120:As java evaluates expressions from "right to left" then why does `++i+i++` returns `6`? As `i++` updates `i` at the end of the expression. *Assuming `i` to be `2` by default* – CᴴᴀZ May 03 '13 at 14:32
  • Sorry for any confusion - I've updated the answer now to explain. Java doesn't evaluate from right to left given equal operator precedence, but in this case the `+` operation takes greater precedence so is evaluated first. – Michael Berry May 03 '13 at 16:03
  • @berry120:Sir Berry, you made me create a new [topic](http://stackoverflow.com/questions/16363112/ii-evaluation?answertab=active#tab-top) -_- – CᴴᴀZ May 03 '13 at 16:16