I got this from Enthuware's
SCJP 6 suite:
The correct output is 6 when you run the above with no arguments. I thought it would be 7, because:
Since the preincrement operator has higher precedence than the assignment operator, that is executed first, and then you have:
k += 3 + 2
Then, the assignment is executed, but at this point k has value 2, after the preincrement operator has executed, so that would give:
k = k + 5 => k = 2 + 5 => k = 7
Instead, it seems the compound assignment operator gets expanded before applying the preincrement. I don't understand this because of the precedence issue. Could anyone explain?
Thanks.
[ December 24, 2008: Message edited by: Ruben Soto ]