Search code examples
javaincrementoperator-precedenceadditionpostfix-operator

Java arithmetic increment operator


int a = 6;
int b  =  a + a++; 
answer is 12

int a = 6;
int  c  = a++ + a; 
answer is 13

why are the answers for b and c are different ? since postfix has higher a precedence value, then the answer for both should be 13 right?


Solution

  • In the first example, you incremented the value of a in the second operand, so it doesn't affect the value of the first operand. But, in the second example, you incremented the value of a in the first operand, so it affects the value of the second operand.