Difference of null and another proposition using this null object

Asked

Viewed 135 times

3

If I have a code, for example:

if(window!=null && window.isOpen()){
   //todo
}

If window for null, Will he still try to call the second proposition or not check anymore? Because if he tries to call, then will give a NullPointerException.

3 answers

11


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.

  • 3

    It would be interesting even to enrich the answer, who denied to tell where the problem is.

5

In order to enter the if, both conditions need to be true, if the first is false(that is to say, windows == null), he will ignore the second and will not enter the if.

Take this example:

String b = null;

if(b != null && b.equals("")){
    System.out.println(b);
}else{
    System.out.println("b não atende as condições do if");
}

The output will be just the display of the message b não atende as condições do if, for b is null.

See working on ideone.

1

  • That’s it, I’m also in doubt, because sometimes on android a similar code was still called...

  • & is a eager operator, means that the whole expression will be evaluated, independent that at the beginning of the expression is already known that the final result may be true or false, he will continue the execution. && is a short-circuit operator, as in @bigown’s reply, it means that if he identifies that regardless of the rest of the expression analysis the result is still X, then he already returns X so as not to waste time with it

  • I believe that answer is partly wrong. The reason is not quite this, or even is, but it is not explaining the reason for not evaluating the second operand. Curiously negatively my answer, someone must be confusing things of what is right and wrong.

  • 1

    I understand that you are in fact wrong. A small portion of it is "true", but it is a collateral issue. The level of performance of the two types of operator is completely different.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.