1

Following the precedence operator in java 8 it is clear that the postfix operator (expr++ expr--) has a higher precedence that the unary operator, pre-unary operator (++expr --expr). However when executing this code:

x = 3; y = ++x - x++;

The value of y is 0

But to me, following the above table, the result should be y = (5 - 3) as x++ should be evaluated first.

Can anyone explain why this is y = 0 and not y = 2?

user207421
  • 305,947
  • 44
  • 307
  • 483
jav
  • 13
  • 5
  • 1
    Operator precedence does not determine the order of evaluation. – Armali Aug 29 '21 at 07:23
  • I saw this link about [What are the rules for evaluation order in Java?](https://stackoverflow.com/questions/6800590/what-are-the-rules-for-evaluation-order-in-java/6801431#6801431) Well, there is an explanation but still is not 100 % clear. Let me ask the question in a different way: When do I use the Operator precedence on the same line in an expression? or why there is an operator precedence order and when is used? – jav Aug 30 '21 at 09:43
  • [tag:postfix-notation] has nothing to do with it. – user207421 Oct 07 '21 at 11:08

1 Answers1

0

When do I use the Operator precedence on the same line in an expression? or why there is an operator precedence order and when is used?

Operator precedence decides which one of several operators is associated with an operand. In the expression ++x - x++ there are two places where precedence comes into play:

  1. ++x - … - The two operators ++ and (binary) - could be used on x; ++ has precedence, so this is equivalent to (++x) - …, not to ++(x - …).
  2. … - x++ - The two operators (binary) - and ++ could be used on x; ++ has precedence, so this is equivalent to … - (x++), not to (… - x)++.
Armali
  • 18,255
  • 14
  • 57
  • 171
  • Thanks @Armali for your response and time. That is understandable, but then I am back to x = 3; y = ++x - x++; Why on this expression x++ has not precedence over '-' operator? That is what I cannot understand – jav Aug 30 '21 at 15:20
  • I mean x++ has not precedence over '-' operator, so then x++ is evaluated before --x – jav Aug 30 '21 at 15:27
  • in other words, why the same logic cannot be used for x = 3; y = ++x - x++; – jav Aug 30 '21 at 16:13
  • 1
    I'm sorry, those phrases don't make sense: (a) _x++ has not precedence_ - A term as _x++_ cannot have precedence, only an operator as _++_ can. (b) _x++ is evaluated before --x_ - There is no _--x_ in the example. – Armali Aug 30 '21 at 17:06
  • As said in the answer, precedence makes clear that in `++x - x++` the pre- and the post-increment operator are used on the respective `x` operand, and consequently the subtraction operator is used on the increment results; precedence has nothing to say about which of the two increments is evaluated first. Regarding the [Java Evaluation Order](https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7): _The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right._ – Armali Aug 30 '21 at 17:07
  • This _evaluation order_ explains the result of your example. First `++x` with x = 3 is evaluated to 4, then `x++` with x = 4 is evaluated to 4, and the subtraction is evaluated to 0. – Armali Aug 30 '21 at 17:13
  • Let my try again please, if you don't mind, so you mentioned: "… - x++ - The two operators (binary) - and ++ could be used on x; ++ has precedence, so this is equivalent to … - (x++), not to (… - x)++." so ++ has precedence over - is this correct? I believe so and it can be seen in the Operator Precedence table. Do we agree? Now, having this logic in mind, how is it possible that this expression is not using the same logic: x = 3; y = ++x - x++; where we have ++ (pre increment), - (subtraction) and ++(post increment). – jav Aug 30 '21 at 19:52
  • To me on this expression, following the table, I should evaluate first the post increment operator, then pre increment operator and finally do the subtraction. Thanks for your time – jav Aug 30 '21 at 19:52
  • 1
    We agree where you ask "Do we agree". But in this where we agree, there is **no** _logic_ at all which would say anything about the order of evaluation. What's more, in your example there is **no** precedence relation between the two increment operators, because they cannot be associated to the same operand - precedence between the two could only exist in an expression like `++x++`. // You cannot tell the order of evaluation just from a precedence table. – Armali Aug 30 '21 at 20:09
  • Consider `a - b`; you'll surely understand that _precedence_ can't tell you whether `a` or `b` is evaluated first. Now, that `a` happens to be `++x` and `b` happens to be `x++` does not change a thing. – Armali Aug 30 '21 at 20:28
  • 1
    Many thanks @Armali, I will digest it bit by bit :) – jav Aug 30 '21 at 21:11