This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I posted the same doubt couple of weeks back. I never got any answer. The i=i++ statement works differently in C and C++. It gives the answer 1. But my reasoning is that post-increment operator is one of the fundamental operators and its behaviour should be the same irrespective of implementation language. I understand the working of i++ statement in Java, but I am still not convinced!!! And I believe that's why most of the people find it so hard to understand the output in Java as majority of us come to Java after a stint in C and C++.
Joined: Apr 25, 2005
yeah dats true, one thing u must remember, c,c++ and java are totally different even at operator level. So never mix them
I got some explantion like this, quite convinced I was with this so sharing it....
What happens with that code is this:
Let's say 'i' has a starting value of 0. The expression 'i++' evaluates to the pre-increment (original) value of 'i', or 0. The result of this expression (0) is remembered. The value of 'i' is then incremented ('i' is now 1). The '=' assignment operator has the least precedence, so it happens last: the result of the right-hand expression (what we remembered earlier, namely 0) is assigned back to 'i'. Therefore, the result of the whole statement is that 'i' has a value of 0.