欢迎光临
我们一直在努力

java中if语句多条件的用法,javaif语句判断三个条件怎么写

Lets say I have this:

if(bool1 && bool2 && bool3) {

}

Now. Is Java smart enough to skip checking bool2 and bool2 if bool1 was evaluated to false? Does java even check them from left to right?

I’m asking this because i was “sorting” the conditions inside my if’s by the time it takes to do them (starting with the cheapest ones on the left). Now I’m not sure if this gives me any performance benefits because i don’t know how Java handles this.

解决方案

Yes, Java (similar to other mainstream languages) uses lazy evaluation short-circuiting which means it evaluates 便宜香港vps 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.

04416561

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。