Java arithmetic increment operator

40 Views Asked by At
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?

1

There are 1 best solutions below

0
Sharofiddin On BEST ANSWER

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.