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.
Okay, short and sweet. You have to understand the concept of a "return value" of a subexpression. Given i++, the return value of this would be the original value of i prior to incrementation; on the other hand, for ++i the return value would be the incremented value of i. The = operator has the lowest priority, so everything on the RHS (right hand side) would have to be resolved first, and the value on the LHS (left hand side) would have to be the return value of the RHS. In the example above, the RHS went first so i was really incremented to 1, but the return value is 0. The LHS therefore receives the value of 0, overwriting whatever previous value was there. The same argument goes whenever you pass a subexpression to a method as a parameter. The value of the parameter is whatever the return value of the subexpression. For example, given
The value of i here is 1, but println() prints out the return value of i++, which is 0. -anthony
Two Examples: 1.- int i = 0; System.out.println(i++); //prints 0 2.- int i = 0; System.out.println(++i); //prints 1 It depends on the position of the incrementor, ++. This also aplies to "--". hope it helps