This week's book giveaway is in the Mac OS forum. We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line! See this thread for details.
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
SCJP - 1.4
SCJD - (B&S) Used 1.5 And It Runs On Solaris10
SCWCD - Thanks to HFSJ!!
Joined: Oct 23, 2004
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
Joined: Jun 22, 2005
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