a += a++ * a++ * a++ in Java. How does it get evaluated?
Asked Answered
M

6

9

I came across this problem in this website, and tried it in Eclipse but couldn't understand how exactly they are evaluated.

    int x = 3, y = 7, z = 4;

    x += x++ * x++ * x++;  // gives x = 63
    System.out.println(x);

    y = y * y++;
    System.out.println(y); // gives y = 49

    z = z++ + z;
    System.out.println(z);  // gives z = 9

According to a comment in the website, x += x++ * x++ * x++ resolves to x = x+((x+2)*(x+1)*x) which turns out to be true. I think I am missing something about this operator precedence.

Magnesium answered 13/11, 2012 at 21:46 Comment(3)
x += 3 * 4 * 5; = 3 + 60 = 63; y = 7 * 7 = 49, z = 4 + 5 = 9Bumpy
Why is this getting voted to close as a duplicate of a C++ question?Bumpy
(I am in agreement with NullUserException - C/C++ have different rules for handling this same construct, but it happens to be well-defined under Java, if not ugly.)Muzzle
M
12

Java evaluates expressions left to right & according to their precedence.

int x = 3, y = 7, z = 4;

x (3) += x++ (3) * x++ (4) * x++ (5);  // gives x = 63
System.out.println(x);

y = y (7) * y++ (7);
System.out.println(y); // gives y = 49

z = z++ (4) + z (5);
System.out.println(z);  // gives z = 9

Postfix increment operator only increments the variable after the variable is used/returned. All seems correct.

This is pseudocode for the postfix increment operator:

int x = 5;
int temp = x;
x += 1;
return temp;

From JLS 15.14.2 (reference):

The value of the postfix increment expression is the value of the variable before the new value is stored.

Malia answered 13/11, 2012 at 21:53 Comment(2)
Thanks for that last statement :) makes it easy to understand how ++x is evaluated. Always got confused with ++x prior to this example.Esprit
'Java evaluates expressions left to right according to their precedence' is a contradiction in terms. Java evaluates operands left to right; operators are evaluated in the order dictated by operator precedence and associativity.Loyola
E
5

Nothing to do with operator precedence per se, just the order of evaluation. Two things to know here:

  1. x++ is the postfix increment, so the value of x is incremented after it is evaluated
  2. * evaluates the right side then the left side.

Considering point 2, the expression x++ * x++ * x++ can be rewritten more specifically as x++ * (x++ * (x++)).

The whole expression can be written as the procedures:

a = x
x += 1
b = x
x += 1
c = a*b
d = x
x += 1
return c*d
Easterling answered 13/11, 2012 at 21:56 Comment(2)
+1, but with nitpick: Actually, operator precedence is in effect everywhere, where you have more than one operator in an expression or statement:) prefix and postfix operators have higher priority than binary operators, and this is why x++ * x++ can be written without parentheses. You'd have to write (x++) * (x++). Without operator precedence and parens, only right-to-left would take place, and it could mean x ++ * (x++)) which for example could be a syntax error: no LHS operand for *Easton
Okay, fair enough. I simply meant that the answer would have more to do with the behaviors of postfix and prefix operators as opposed to operator precedence as would be the case with an expression that mixed many operators of different precedence (eg. x++ * x++ + x++ / x++)Easterling
D
2

The postfix operator x++ means something like "give me the value of x now, but increment it for future references"

So, by the order of operations and evaluation,

x++ * x++ * x++

is interpreted first as

3 * 4 * 5 (=60)

Which is then added to the original 3, yielding 63.

The original value is used because it's on the same line, had you written something like:

int x = 3;

int y += x++ * x++ * x++; 
x += y;

x would now be 66, instead of 63 because the x in the second line is now 6, rather than its original 3.

Dvorak answered 13/11, 2012 at 21:54 Comment(0)
K
2

Because the increment Operation ++ is added after the variable x. That's a post increment operation. That means, x is incremented after the operation is handled.

In your example the expression would be: 
x += 3 * 4 * 5
First the expression is added by 3 (x+=....)
then the first x++ results in 3
the second x++ results in 4 (because it was incremented before)
and the third x++ results in 5.

If you want your variable incremented before the operation is executed, you have to write ++x (pre increment operation)

Kayser answered 13/11, 2012 at 22:11 Comment(0)
M
1

Because a postincrement modifies the variable after the value is taken and += evaluates its left hand side before evaluating its right hand side,

x += x++ * x++ * x++; 

becomes

tmp0 = x

tmp1 = x
++x
tmp2 = tmp1 * x
++x
tmp3 = tmp2 * x
++x

x = tmp0 + x
Maladapted answered 13/11, 2012 at 21:53 Comment(2)
Are you sure yo meant the left hand side gets evaluated BEFORE the right hand side, because I think I knew the opposite ? The code breakup is good, except that the last statement x = tmp0 + x doesn't result in 63 for x = 3.Magnesium
Otherwise it would be 66 instead of 63.Maladapted
E
1

unary operators evaluated left to right, so the first x++ gets the value x, the second is (x+1), etc. And the += evaluates according to the value of x at the start, hence the addition of x

Extradite answered 13/11, 2012 at 21:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.