我只是想知道为什么我们通常在两个布尔值之间使用逻辑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有|=,但没有||=

必须使用||的一个例子是,当第一个表达式是测试第二个表达式是否会爆炸时。例:在以下情况下使用单个|可能导致NPE。

public static boolean isNotSet(String text) {
   return text == null || text.length() == 0;
}

其他回答

| is the binary or operator

|| is the logic or operator

||通过OR'ing两个值返回布尔值(这就是为什么它被称为逻辑或)

IE:

if (A || B) 

如果A或B中有一个为真则返回真,如果它们都为假则返回假。

|是一个对两个值执行位操作的运算符。为了更好地理解位操作,你可以阅读这里:

http://en.wikipedia.org/wiki/Bitwise_operation

如果使用||和&&形式,而不是这些操作符的|和&形式,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));
}

它并不是特别有用,并且(与大多数编程任务一样)您可以通过其他方法实现它。但这是位操作数的一个用例。