Does the order of operands in a Boolean affect the order of evaluation? As an example can I rely on
String s = ...;
if (s != null && s.length() > 10) ...
I seem to remember that the Java language does not define order of evaluation and that a null value of s could cause a NPE. Is this still true (a) in principle and (b) in practice with the common Java implementations?
UPDATE:
I couldn’t find the exact duplicate. It’s interesting to see the differences of opinions in the answers and get a downvote for it – ah well!
Yes, it does matter and
is not true. The arguments are evaluated from left to right, so an expression like
s != null && s.length() > 10will never throw aNullPointerException.See 15.7 Expressions – Evaluation Order in the Java Language Specification.