The second will not be called. That’s called short Circuit Evaluation (in Portuguese). When the expression already gets a final guarantee value, there is no reason to keep checking the rest and the execution ends. In the case how the relational operator is an "AND" and the two operands need to be true to result in true
, if the first is already false
the expression is known to be false
.
This is equivalent to:
if (window != null) {
if (window.isOpen()) {
//todo
}
}
I put in the Github for future reference.
but it’s a more concise way.
This is the correct technique, and quite used, to ensure that the object is not null. First it checks if it is null and then an operator "E" does what you want, which will only be executed if the first one is true. You can be creative with a lot of stuff in there and use the same technique for a lot of things.
There are cases where you use the ||
. in that case if the first true
the second will not be executed, since in an operator "OR" just one true being need not check the other operand.
It would be interesting even to enrich the answer, who denied to tell where the problem is.
– user28595