The second will not be called. This is called short circuit evaluation (in Portuguese ). When the expression already obtains a final guaranteeing value, it does not have to continue to check the rest and the execution terminates. In case the relational operator is an AND and the two operands must be true to result in true
, if the first one is already false
, it is already known that the expression is false
.
This is equivalent to:
if (window != null) {
if (window.isOpen()) {
//todo
}
}
But it's a more concise form.
This is the correct, well-used technique to ensure that the object is not null. First it is verified if it is null and after an "And" operator does what it wants, that will only be executed if the first one is true. You can be creative with a lot of things there and use the same technique for various things.
There are cases where ||
is used. in this case if the first one is true
the second one will not be executed, since in an "OR" operator a true one is enough to neither need to check the other operand.