我只是想知道为什么我们通常在两个布尔值之间使用逻辑OR ||,而不是按位或|,尽管它们都工作得很好。
我的意思是,看看下面这些:
if(true | true) // pass
if(true | false) // pass
if(false | true) // pass
if(false | false) // no pass
if(true || true) // pass
if(true || false) // pass
if(false || true) // pass
if(false || false) // no pass
我们可以用|代替||吗?&和&&也是一样。
如果使用||和&&形式,而不是这些操作符的|和&形式,Java将不会费心单独计算右操作数。
这是一个你是否想要缩短评估的问题——大多数时候你想这样做。
说明短路的好处的一个好方法是考虑下面的例子。
Boolean b = true;
if(b || foo.timeConsumingCall())
{
//we entered without calling timeConsumingCall()
}
正如Jeremy和Peter提到的,短路的另一个好处是空引用检查:
if(string != null && string.isEmpty())
{
//we check for string being null before calling isEmpty()
}
更多信息
逻辑||和&&只在必要时检查右边。|和&每次检查两边。
例如:
int i = 12;
if (i == 10 & i < 9) // It will check if i == 10 and if i < 9
...
重写:
int i = 12;
if (i == 10 && i < 9) // It will check if i == 10 and stop checking afterward because i != 10
...
另一个例子:
int i = 12;
if (i == 12 | i > 10) // It will check if i == 12 and it will check if i > 10
...
重写:
int i = 12;
if (i == 12 || i > 10) // It will check if i == 12, it does, so it stops checking and executes what is in the if statement
...
非短路是有用的。有时你想确保两个表达式都有值。例如,假设您有一个从两个单独的列表中删除对象的方法。你可能想这样做:
class foo {
ArrayList<Bar> list1 = new ArrayList<Bar>();
ArrayList<Bar> list2 = new ArrayList<Bar>();
//Returns true if bar is removed from both lists, otherwise false.
boolean removeBar(Bar bar) {
return (list1.remove(bar) & list2.remove(bar));
}
}
如果您的方法改为使用条件操作数,如果第一个列表返回false,则它将无法从第二个列表中删除对象。
//Fails to execute the second remove if the first returns false.
boolean removeBar(Bar bar) {
return (list1.remove(bar) && list2.remove(bar));
}
它并不是特别有用,并且(与大多数编程任务一样)您可以通过其他方法实现它。但这是位操作数的一个用例。