0
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
  • Look here: introcs.cs.princeton.edu/java/11precedence. Note that postfix and prefix operators both of higher precedence than "+" (like you said).. Note, too, that postfix is left to right (hence "7 + 6"), like you'd expect... but prefix is right to left (hence 6 + 6, then increment). Commented Mar 25, 2023 at 6:01

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.