|René Nyffenegger's collection of things on the web|
René Nyffenegger on Oracle - Most wanted - Feedback
- Follow @renenyffenegger
Precedence of the modulus (reminder) and the ++ (incrementing) operator in Java and C++
I do C++ programming a lot and occasionally, I do Java. One of the idioms that I used often (at least in C++) is
Assuming that a is greater than or equal to 0, then this idiom increments the value of a by one. If a's value reaches 7, it is set to 0 again, making sure that a is always between 0 and 6 (inclusively).
That is, in C++. In Java, this idiom does not change the value of a at all (still assuming the value of a being between 0 and 6 (inclusively).
I just lost an hour finding a 'bug' because this idiom is treated differently in Java than in C++. Oh yeah, don't assume anything.
Frederic Barachant comments on this article.