-2

Say we have Java code below:

int x = 1;
int y = x + x++;

'Cause the precedence of "postfix-increment" operator is higher than "add" operator. I think x++ should be calculate and evaluated first. Then it comes to x + (x++). So the y should be "new_x(2) + 1 = 3".

Though it turns out "2" instead of "3". Any ideas? Thanks.


This question is greatly different from Is there a difference between x++ and ++x in java?. That question mentions nothing about the operator precedence.

Someone has explained that expression is read from left to right. Does it conflict with the precedence of those operators?

Just as I mentioned before, I think x++ should be calculated first. It seems that I mess up with the "evaluation" and "calculation".

Community
  • 1
  • 1
Qi W.
  • 706
  • 9
  • 21
  • 4
    Expressions are read from left to right. `x + x++;` --> `1 + x++`. And when `x = 1`, `x++` will return 1. So the result is `1 + 1 = 2`. – Tunaki Mar 18 '16 at 16:46
  • 1
    @Bathsheba Would you have prefered this dupe? http://stackoverflow.com/q/13369393/1743880 – Tunaki Mar 18 '16 at 16:48
  • It's not a bad one, but perhaps a better one could be found, else you could always answer it. A good answer needs to talk about associativity and precedence. I liked your comment a lot. – Bathsheba Mar 18 '16 at 16:51
  • `x++` is evaluated first, yes, but result of `x++` is the value of `x` prior to evaluation. – Alex Salauyou Mar 18 '16 at 16:51
  • @Bathsheba There are a lot of duplicates out there, the one I linked to actually talks about the same case `y = y * y++;`. – Tunaki Mar 18 '16 at 16:53
  • `x++` is the same as `(x = x + 1)` **but** the assignment happens *after* the current statement (that's why it is called a [*post-increment*](https://en.wikipedia.org/wiki/Increment_and_decrement_operators) operator). So, `y = x + x++` is the same as `y = 1 + 1; x = x + 1;` and thus you get `x` equal to `2` **and** `y` equal to `2`. – Elliott Frisch Mar 18 '16 at 16:53
  • @Tunaki: for me, a duplicate is a duplicate, not an approximate match. And y = y * y++ is different from x = y * y++. – Bathsheba Mar 18 '16 at 16:54
  • 1
    Another one http://stackoverflow.com/q/18991306/1743880 – Tunaki Mar 18 '16 at 16:58
  • @Tunaki The associativity of `*` is left-to-right. Assume `y = y * y++`. Yes, according to associativity, y is evaluated first. But what about the precedence, 'cause ++ still has the higher precedence. Why y++ cannot be evaluated first? – Qi W. Mar 18 '16 at 17:02
  • @StephenW Take a look at the other question I linked to (the comment just above). From it: *The confusion stems from the fact that the operands are evaluated from left to right. This is done first, before any attention is paid to operator precedence/order of operations.* – Tunaki Mar 18 '16 at 17:05
  • @Tunaki Thanks. I will look at it at once. – Qi W. Mar 18 '16 at 17:06

2 Answers2

1

The difference between x++ and ++x is quite evident and known. ++ can be used as preadd (Add 1 and then use x ) or postadd (use x and add 1). Check this out.

As tested and seen

1. x + x++ will give 2
2. x + ++x will give 3
3. x++ + x will give 3
4. ++x + x will give 4

Now the interesting case here is 2nd and 4th which basically explains pre-add operator.

In these cases, the value of x is incremented first and then used in both the places.

However in case of test 2 the first operand i.e. x is already used up for addition so the answer is 3

while in the test 4 the operand x is first incremented and used in both the operands. This happens because the expressions are evaluated from left to right.

Milind Gokhale
  • 575
  • 2
  • 14
-1

x++ would first return x then increment x.

++x would increment x first then return x.

Therefore :

int y = x + x++; //y (2) = x(1) + x(1, then becomes 2 right after)

int y = x + ++x; //y (3) = x(1) + x(becomes 2 before being returned)
Benoit Vanalderweireldt
  • 2,925
  • 2
  • 21
  • 31