Dear Ranch People
Uttara Rishis contribution concerning the increment operator includes an aspect of operator behavior I discussed with a colleague a few weeks ago: Let "i" be an integer variable; how to predict the outcome of "i++ + ++i" with respect to
Java?
According to Les Hattons "Safer C" (ISBN 0077076400) "in the example a*(f()+g()) although the addition is done before the multiplication, the order in which f() and g() are evaluated is undefined"; see subsection 2.8.2 "Evaluation order and precedence" on page 71 near the bottom.
Assume, that this C behavior applies to Java, we can't tell about the result of "i++ + ++i". Set "i=0". If "i++" is evaluated before "++i", the result is 0+1=1. If "++i" is evaluated first, the result is 1+1=2. Bottom line: We must not use expressions like "i++ + ++i" in our code.
This reply is intended to be both a thought-provoking impulse and a question: Does Java behave the same way like C? Could I ask you to provide a reference where this is declared officially, in either case? Can the compiler be invoked in order to detect situations like this and print a warning?
Best regards,
Ralf