Why do we usually use `||` not `|`, what is the di

2020-01-25 11:42发布

I'm just wondering why we usually use logical OR || between two booleans not bitwise OR |, though they are both working well.

I mean, look at the following:

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

Can we use | instead of ||? Same thing with & and &&.

28条回答
干净又极端
2楼-- · 2020-01-25 11:58

A side note: Java has |= but not an ||=

An example of when you must use || is when the first expression is a test to see if the second expression would blow up. e.g. Using a single | in hte following case could result in an NPE.

public static boolean isNotSet(String text) {
   return text == null || text.length() == 0;
}
查看更多
神经病院院长
3楼-- · 2020-01-25 11:59

usually I use when there is pre increment and post increment operator. Look at the following code:

package ocjpPractice;
/**
 * @author tithik
 *
 */
public class Ex1 {

    public static void main(String[] args) {
    int i=10;
    int j=9;
    int x=10;
    int y=9;
    if(i==10 | ++i>j){
        System.out.println("it will print in first if");  
        System.out.println("i is: "+i);
    }

    if(x==10 ||++x>y){
        System.out.println("it will print in second if");   
        System.out.println("x is: "+x);
    }
    }
}

output:

it will print in first if
i is: 11

it will print in second if
x is: 10

both if blocks are same but result is different. when there is |, both the conditions will be evaluated. But if it is ||, it will not evaluate second condition as the first condition is already true.

查看更多
爷、活的狠高调
4楼-- · 2020-01-25 12:00

Java operators

| is bitwise or, || is logical or.

查看更多
The star\"
5楼-- · 2020-01-25 12:02

Non short-circuiting can be useful. Sometimes you want to make sure that two expressions evaluate. For example, say you have a method that removes an object from two separate lists. You might want to do something like this:

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));
    }
}

If your method instead used the conditional operand, it would fail to remove the object from the second list if the first list returned false.

//Fails to execute the second remove if the first returns false.
boolean removeBar(Bar bar) {
    return (list1.remove(bar) && list2.remove(bar));
}

It's not amazingly useful, and (as with most programming tasks) you could achieve it with other means. But it is a use case for bitwise operands.

查看更多
混吃等死
6楼-- · 2020-01-25 12:02

After carefully reading this topic is still unclear to me if using | as a logical operator is conform to Java pattern practices.

I recently modified code in a pull request addressing a comment where

if(function1() | function2()){
  ...
}

had to be changed to

boolean isChanged = function1();
isChanged |= function2();
if (isChanged){
  ...
}

What is the actual accepted version?

Java documentation is not mentioning | as a logical non-shortcircuiting OR operator.

Not interested in a vote but more in finding out the standard?! Both code versions are compiling and working as expected.

查看更多
我欲成王,谁敢阻挡
7楼-- · 2020-01-25 12:04
| is the binary or operator

|| is the logic or operator
查看更多
登录 后发表回答