1

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 ?

6
  • neither its hard to be ironic... My answer to your ironic question is that I couldn't explain this better in order to google it. Commented Feb 4, 2014 at 20:16
  • 2
    Read about what && operator does. You'll get your answer. Commented Feb 4, 2014 at 20:16
  • I know, but will it stop the statement the first time it hits false? Commented Feb 4, 2014 at 20:17
  • && will stop on the first false condition. & instead will evaluate all conditions. Commented Feb 4, 2014 at 20:18
  • 1
    @user3236641 You know what? If you know what && is, then you shouldn't really be asking this question. Anyways, && is short-circuit operator. It will not evaluate the 2nd expression, if 1st one is false. Commented Feb 4, 2014 at 20:18

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I feel like this guy has trouble finding the docs, not reading them. It can be hard for beginners to do a meaningful search without knowing the proper terminology. Kind of a vicious cycle really.
I feel like the OP is just lazy. I know a lazy developer when I see one. I didn't ask anyone what something meant, I google it or read it in a tutorial, docs etc. And if you're lazy to run the code to see for yourself what then, every 5 lines of code you'll ask someonelse if you wrote the right code?
1

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) {...}

Comments

0
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

1 Comment

@user3236641 so you got
0

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.

Comments

0

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?

2 Comments

What do you mean with '&& takes care'?
@C-Otto Clarified for better understanding. After OP understood, I didn't care to explain more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.