本文介绍了在布尔结果已知后,Java 是否评估剩余条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,如果我有一个评估多个条件的语句,比如像这样的或"语句..

That is, if I have a statement that evaluates multiple conditions, in say a 'or' statement like so..

if(isVeryLikely() || isSomewhatLikely() || isHardlyLikely())
{
    ...
}

如果isVeryLikely()在运行时返回true,isSomewhatLikely()isHardlyLikely()会执行吗?如果它们是静态布尔值而不是方法呢?

In the case that isVeryLikely() returns true at runtime, will isSomewhatLikely() and isHardlyLikely() execute? How about if instead of methods they were static booleans?

推荐答案

||&& 运算符是短路的.

The || and && operators are short-circuiting.

true || willNeverExecute();
false && willNeverExecute();

这篇关于在布尔结果已知后,Java 是否评估剩余条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:07