-2

For the next code, what it's z? (Java)

int x = 5;
int y = 10;
int z =++x*y--;

The order of priority is: y--, ++x, *, =. ( https://introcs.cs.princeton.edu/java/11precedence/ ) Why after run the code, z = 60 ?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343

2 Answers2

0

The ++ operator is evaluated before the expression.

i.e.:

int x = 10;
int y = ++x; //y = 11
int z = x ++; // z = 11;
Hongyu Wang
  • 378
  • 1
  • 10
0

y-- is higher up on the list from your source. However, when the post-decrement happens, it happens after the whole evaluation.

So if you print y after getting the value of z, it will be 9.

And the pre-increment happens first, so ++x becomes 6 within that statement (and obviously multiplies by 10).

See an example in the docs.

achAmháin
  • 4,176
  • 4
  • 17
  • 40