How does Java deal with multiple conditions inside a single IF statement

前端 未结 4 1841
谎友^
谎友^ 2020-12-15 21:40

Lets say I have this:

if(bool1 && bool2 && bool3) {
...
}

Now. Is Java smart enough to skip checking bool2 and bool2 if boo

4条回答
  •  再見小時候
    2020-12-15 22:20

    Yes, Java (similar to other mainstream languages) uses lazy evaluation short-circuiting which means it evaluates as little as possible.

    This means that the following code is completely safe:

    if(p != null && p.getAge() > 10)
    

    Also, a || b never evaluates b if a evaluates to true.

提交回复
热议问题