Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we usually use || over |? What is the difference?

People also ask

What is || used for?

The logical OR ( || ) operator (logical disjunction) for a set of operands is true if and only if one or more of its operands is true. It is typically used with boolean (logical) values. When it is, it returns a Boolean value.

What is the difference between || and &&?

The && and || Operators in JavaScript. If applied to boolean values, the && operator only returns true when both of its operands are true (and false in all other cases), while the || operator only returns false when both of its operands are false (and true in all other cases).

What's the difference between and || in Java?

Differences between | and || operators in Java| is a bitwise operator and compares each operands bitwise. It is a binary OR Operator and copies a bit to the result it exists in either operands. (A | B) will give 61 which is 0011 1101. Whereas || is a logical OR operator and operates on boolean operands.

Why do we use || in C?

Logical OR (||) operator in C Logical OR is denoted by double pipe characters (||), it is used to check the combinations of more than one conditions; it is a binary operator – which requires two operands.


If you use the || and && forms, rather than the | and & forms of these operators, Java will not bother to evaluate the right-hand operand alone.

It's a matter of if you want to short-circuit the evaluation or not -- most of the time you want to.

A good way to illustrate the benefits of short-circuiting would be to consider the following example.

Boolean b = true;
if(b || foo.timeConsumingCall())
{
   //we entered without calling timeConsumingCall()
}

Another benefit, as Jeremy and Peter mentioned, for short-circuiting is the null reference check:

if(string != null && string.isEmpty())
{
    //we check for string being null before calling isEmpty()
}

more info


| does not do short-circuit evaluation in boolean expressions. || will stop evaluating if the first operand is true, but | won't.

In addition, | can be used to perform the bitwise-OR operation on byte/short/int/long values. || cannot.


So just to build on the other answers with an example, short-circuiting is crucial in the following defensive checks:

if (foo == null || foo.isClosed()) {
    return;
}

if (bar != null && bar.isBlue()) {
    foo.doSomething();
}

Using | and & instead could result in a NullPointerException being thrown here.


Logical || and && check the right hand side only if necessary. The | and & check both the sides everytime.

For example:

int i = 12;
if (i == 10 & i < 9) // It will check if i == 10 and if i < 9
...

Rewrite it:

int i = 12;
if (i == 10 && i < 9) // It will check if i == 10 and stop checking afterward because i != 10
...

Another example:

int i = 12;
if (i == 12 | i > 10) // It will check if i == 12 and it will check if i > 10
...

Rewrite it:

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
...