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 ?

Nathaniel Jones
  • 1,829
  • 3
  • 29
  • 27
user3236641
  • 37
  • 1
  • 4
  • 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. – user3236641 Feb 04 '14 at 20:16
  • 2
    Read about what `&&` operator does. You'll get your answer. – Rohit Jain Feb 04 '14 at 20:16
  • I know, but will it stop the statement the first time it hits false? – user3236641 Feb 04 '14 at 20:17
  • `&&` will stop on the first false condition. `&` instead will evaluate all conditions. – bobbel Feb 04 '14 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`. – Rohit Jain Feb 04 '14 at 20:18
  • well, these things they don't teach us in college... I just wanted to be sure thats all. – user3236641 Feb 04 '14 at 20:21

5 Answers5

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.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • 5
    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. – Mad Physicist Feb 04 '14 at 20:21
  • 1
    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? – MikeSW Feb 04 '14 at 20:24
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) {...}
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?

Community
  • 1
  • 1
Prince
  • 20,353
  • 6
  • 39
  • 59
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

Girish
  • 1,717
  • 1
  • 18
  • 30
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.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257