Any of you experts correct me if I am wrong, but what is happening here is the first operation is that we are incrementing i from 10 to 11, then adding it to i again which would otherwise still be 11, but since the decremenation occurs first, i then becomes 10 again. Thus 11 + 10 = 21. I hope that helps. --Bob

Bob N
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!

Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151

posted

0

I apologize. Your topic read "What is the value...?" Your post, in essence, asked, "Why is the value...?" For "what is the value?" questions, the best answer always comes from your Java compliant JVM. This forum is good for "Why is the value...?" questions.

In short, I believe you have misunderstood "associativity" and "order of operation."

++ or -- operator takes more precedence over + operator So the sequence of operation would be like this int i =10; "++i + --i" piece will be step 1 ++i (so i becomes 11) step 2 --i(so then i becomes 10) the adding operands 11+10=21 Hope this helps

Sayan

satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63

posted

0

I know the answer is 21. But WHy..

Do the Unary Prefix operators(++a,--a) associate from right to Left?

In the example ++,-- has higher precedence than '+'.

Then between ++,-- , they have right to left associativity..

dear satya, there is a difference in associativity and evaluation order. Here the problem is related to evaluation order and not associativity. every operand is evaluated from L to R; so it would be --> ++i + --i --> 11 + --i --> 11 + 10; --> 21