0

I know that the following question has been asked several times already but I never encountered a satisfying answer as to why the priority of operators seems to change between following 2 scenario's:

Scenario 1:

  int i = 0, j = 0;
  j = i++;  
  System.out.printf("i=%d j=%d \n", i, j);   // i=1 j=0 

The operation j = i++ evaluates, according to the priority of operators, as follows:

step 1: j = i

step 2: i++

'++' operator AFTER '=' operator The output seems to confirm this. So far so good.

Scenario 2:

int i = 0;
i = i++;
System.out.printf("i=%d \n", i);    // i=0 ???

The operation i = i++ evaluates, NOT according to the priority of operators, as follows:

step 1: i++

step 2: i = i

'++' operator BEFORE '=' operator ???

What am I missing here?

regards Chris

ChrisPeeters
  • 153
  • 2
  • 13
  • Also see [this duplicate](http://stackoverflow.com/questions/21234823/why-does-post-incrementing-have-higher-precedence-than-pre-incrementing-in-java) – Hovercraft Full Of Eels Oct 19 '16 at 19:08
  • The way I've always thought of it -- post-decrement means you're going to first USE the variable, then after you've used it, increment it. So you use the 0 in the assignment statement, then you increment the variable. Makes perfect sense to me, given what post-decrement started out as in the first place. If there's any question, I start putting in parentheses... – arcy Oct 19 '16 at 19:20

0 Answers0