3 ms·
Unlike in C or C++, the order of evaluation in Java is well defined, so there's no problem depending on it.
by cesarb 8y ago
Unlike in C or C++, the order of evaluation in Java is well defined, so there's no problem depending on it.
- chopin 8y agoDo you have a reference for this? If I remember correctly the Java spec says nothing definite about evaluation order and the memory model mentions reordering explicitely ("happens before" relationships rely on a memory barrier; without that, no guarantees).
- cesarb 8y agoJust open the Java Language Specification and look at the table of contents: chapter 15.7 is titled "Evaluation Order" and specifies it in detail.
- chopin 8y agoThanks. What I had in mind applies to distinct statements not expressions. Funnily the chapter starts with the following caveat: It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.