简体   繁体   中英

Java null check on object

Okay, its a simple question, I dont have any problem with it but I just want to be sure. Is it correct if I do something like this ?

if (person != null && person.getAge > 20) {...}

can I do this? null check and object access on the same line? is this correct? or I have to make an inner statement on the null check statement like:

if (person != null)
{
    if (person.getAge() > 20) {...}
}

imo, the first statement wouldnt throw a NPE, but I needed feedback from professionals...

Generally my question is that, on if statements with &&, if the first boolean is false, will the JVM stop executing the if statement or it will check it all ?

Yes. The operators && and || are what are known as short circuiting operators. This means that as soon as the answer is known, reading from left to right, the other statements in the condition are not evaluated. In your case, as soon as person != null part evaluates to false , the whole condition will be guaranteed to be false regardless of the other statements, so person.getAge > 20 will not be evaluated and will not throw an exception. Similarly, if you use || , and the first part evaluates to true , the second part will not be evaluated because the overall conditional is guaranteed to be true at that point.

yes you can do that. if person is null, it will see

if(person != null && otherBoolean  )

if person != null is false, than it doesn't matter what otherBoolean is, because the expression will be guaranteed to be false no matter what.

It evaluates the leftmost expression first, so if you had it in the other order, that would't work.


as a side note, you can test this pretty easily by writing:

person = null;

if (person != null && person.getAge > 20) {...}

It's called short-circuiting and it's perfectly fine to do that.

if (person != null && person.getAge > 20) {...}

Always make sure you do a null check and && will move to check the next condition only if the previous one is true .

In this case person.getAge > 20 is checked only if person != null is true .


Also check this SO question: Java logical operator short-circuiting

For further reading: Avoiding “!= null” statements in Java?

if (person != null && person.getAge > 20) {...}//good to go with that

Surely in this case if the person is null it will not evaluate person.getAge > 20 so you will not get NPE

Read about the && operator in java surely it will not check the second value

if (person != null && person.getAge > 20) {...}

&& is short circuit logical AND. Thus if person != null is false, person.getAge > 20 will not be evaluated.

& is logical AND that does not short circuit. If you use if (person != null & person.getAge > 20) and person is null, you will get a null pointer exception when person.getAge > 20 is evaluated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM