0

I don`t understand how Java is progressing this arithmetic expression

int x = 1;
int y = 1;
x += y += x += y;
System.out.println("x=" + x + " y=" + y);

With Java I get x = 4 and y = 3. But in C, Perl, Php I get x=5 and y = 3 On the paper I also get x = 5 and y = 3

4
  • 7
    Don't assume that all languages treat statements with side effects in the same way. Java isn't getting it "wrong" - it's just defined differently to other languages. You should avoid this sort of code anyway. Commented Jan 31, 2014 at 16:41
  • 6
    That's because in Java, the behavior of such horrible code is well-defined, whereas in C at least, it's not. Read the Java Language Spec, or decide that it's useless, because anyone writing code like this should be fired anyway. Commented Jan 31, 2014 at 16:42
  • I see x = 4 and y = 3. For me that's perfect. Don't know how perl, php and C manage this, but for me looks fine. Commented Jan 31, 2014 at 16:42
  • 2
    IIRC this is actually undefined behavior in C so what it does in C is totally meaningless. Commented Jan 31, 2014 at 16:47

2 Answers 2

9

This is the nasty part, obviously:

x += y += x += y;

This is executed as:

int originalX = x; // Used later
x = x + y; // Right-most x += y
y = y + x; // Result of "x += y" is the value stored in x
x = originalX + y; // Result of "y += x" is the value stored in y

So:

                     x      y
(Start)              1      1
x = x + y            2      1
y = y + x            2      3
x = originalX + y    4      3

The important part is the use of originalX here. The compound assignment is treated as:

x = x + y

and the first operand of + is evaluated before the second operand... which is why it takes the original value of x, not the "latest" one.

From JLS section 15.16.2:

If the left-hand operand expression is not an array access expression, then:

  • First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.

  • Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

When in doubt, consult the language specification - and never assume that just because two languages behave differently, one of them is "wrong". So long as the actual behaviour matches the specified behaviour for each language, all is well - but it does mean you need to understand the behaviour of each language you work with, of course.

That said, you should clearly avoid horrible code like this in the first place.

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

Comments

-1

it starts from right to left

1. x += y gives x = 2, y = 1
2. y += ( x +=y ) gives x = 2, y = 3
3. x += ( y += x += y ) gives x = 4, y = 3

so, it's all consistent

6 Comments

Your step 2 results are wrong - when/how did x go back to being 1? It should show x = 2, y = 3. (That's what you'd get if the whole statement was just y += x += y;) At that point you need to explain why the final value is 4... which takes a bit more work.
But you haven't explained why x += ... gives an answer of 4, not 5. Naively, one might think that if the value of x is 2, and the value of y is 3, then x += y will give x = 5. The important part is that x is evaluated before any of the rest happens and that's the value which is added to in the last step.
I put it short, you put it long. what's the problem?
You didn't "put it" at all, in my view. A language could be specified such that the compound assignment operator evaluates the right hand side first, then evaluates the variable and adds the relevant amount - everything in your answer would still apply, but it would give a different result.
I answered the question as briefly as it was asked. Of course you can go deeply in details and start with primitive types and variable definitions if you like. It's just another level. Someone is looking for short answers, others for longer ones. If both of them are correct it's fine.
|

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.