1

Suppose we have the following code snippet in Java:

int a = 3;
int b = a++;

The value of a gets assigned to b first and then it gets incremented. b = 3 and a = 4. Then why does the postfix expression get evaluated from right to left? Wouldn't it make more sense for it to be evaluated from left to right?

Ahmad Y. Saleh
  • 3,309
  • 7
  • 32
  • 43
Prachi
  • 528
  • 8
  • 31
  • How is the ternary operator evaluated from right to left? Surely for `a?b:c` the evaluation order is `a` then either `b` or `c`, depending on the value of `a`. – biziclop Apr 28 '14 at 15:53
  • Evaluation order only applies if there are multiple things to evaluate. Your example doesn't. – Sotirios Delimanolis Apr 28 '14 at 15:54

3 Answers3

2

The postfix operators are not evaluated right-to-left; their side-effect happen after the value is determined (roughly speaking - the JLS specifies the order).

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
  • I am reading OCA Java SE 7 Associate Study Guide. Chapter 3 of the book clearly says that the postfix increment and decement operators are evaluated from right to left. I also verified the same from http://cseweb.ucsd.edu/~ricko/CSE8A/Java_Operator_Precedence_Table.pdf – Prachi Apr 28 '14 at 15:55
  • 1
    That's associativity, not evaluation order. – biziclop Apr 28 '14 at 15:58
  • Thanks! That helped clarify everything... http://stackoverflow.com/questions/930486/what-is-associativity-of-operators-and-why-is-it-important gives an excellent explanation of operator associativity. – Prachi Apr 28 '14 at 16:12
  • @Prachi You're welcome, I've tried to give a not-so-excellent one here too. :) – biziclop Apr 28 '14 at 16:17
0

Actually: No. This is the expected behaviour. Compare a++ with ++a. If you would do the last one, you would get a=4, b=4.

nils
  • 1,362
  • 1
  • 8
  • 15
  • Well it does make sense for the prefix operators to get evaluated from right to left. Update a first and then b. But in the postfix operators, b is getting updated before a. So it makes more sense to have it read from left to right... at least to me. – Prachi Apr 28 '14 at 15:57
0

There seems to be some kind of confusion here. Java always evaluates from left to right (or to be more precise, always pretends to do so), as stated in the JLS here:

The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

What you're talking about is the direction of associativity, that is if an operand is sandwiched between two operations of the same precedence, which one is the operand bound to. So the ternary operator for example is right-associated because if you've got a?b?c:d:e, you expect it to resolve to a?(b?c:d):e

But the postfix ++, on the same precedence level as the '.' operator is left-to-right associated, which is why foo.bar++ works.

The prefix ++ is indeed right grouping because ++++x should really be `++(++x). Since it's a unary operator and the operand is to the right, it must group to the right.

biziclop
  • 48,926
  • 12
  • 77
  • 104