Asked  10 Months ago    Answers:  5   Viewed   634 times

The contents of both of the following if blocks should be executed:

if( booleanFunction() || otherBooleanFunction() ) {...}
if( booleanFunction() | otherBooleanFunction() ) {...}

So what's the difference between using | or using ||?

Note: I looked into this and found my own answer, which I included below. Please feel free to correct me or give your own view. There sure is room for improvement!



The logical operator works on booleans, and the bitwise operator works on bits. In this case, the effect is going to be the same, but there are two differences:

  1. The bitwise operator is not meant for that, which makes it harder to read but most importantly
  2. The logical OR operator will evaluate the first condition. If it's true, it does not matter what the next condition results in, the result will be true, so the second clause is not executed

Here's some handy code to prove this:

public class OperatorTest {

    public static void main(String[] args){
        System.out.println("Logical Operator:");
        if(sayAndReturn(true, "first") || sayAndReturn(true, "second")){

        System.out.println("Bitwise Operator:");
        if(sayAndReturn(true, "first") | sayAndReturn(true, "second")){

    public static boolean sayAndReturn(boolean ret, String msg){
        return ret;
Sunday, August 8, 2021

Assuming that those values are primitive types, then no, there's no difference. Initialization lists only make a difference when you have objects as members, since instead of using default initialization followed by assignment, the initialization list lets you initialize the object to its final value. This can actually be noticeably faster.

Tuesday, June 1, 2021

Those are the bitwise AND and bitwise OR operators.

int a = 6; // 110
int b = 4; // 100

// Bitwise AND    

int c = a & b;
//   110
// & 100
// -----
//   100

// Bitwise OR

int d = a | b;
//   110
// | 100
// -----
//   110

System.out.println(c); // 4
System.out.println(d); // 6

Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.

Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to the Conditional-And (&&) and Conditional-Or (||) operators except for the fact that they don't short-circuit so while the following is safe:

if((a != null) && (a.something == 3)){

This is not:

if((a != null) & (a.something == 3)){

"Short-circuiting" means the operator does not necessarily examine all conditions. In the above examples, && will examine the second condition only when a is not null (otherwise the whole statement will return false, and it would be moot to examine following conditions anyway), so the statement of a.something will not raise an exception, or is considered "safe."

The & operator always examines every condition in the clause, so in the examples above, a.something may be evaluated when a is in fact a null value, raising an exception.

Tuesday, June 1, 2021

The latter is only available since Java 7. That's why you often see the former. The latter is equivalent, and shorter.

Saturday, July 31, 2021

The | and & operators are bitwise operations on integers and eager logic operators on Booleans. The || and && operators are lazy logic operators on Booleans.

Also, when using the && and || operators on a class, why do | and & have to be overloaded? Why can't just the true and false values for a class be overloaded?

You have a class Foo with an overloaded true and false operator. You wish the && operator on Foo to take two Foos and return a third. Explain how you plan to do so with only the true and false operators.

why do we need eager Boolean logic operators in C#?

In case you want to evaluate the side effects of two Boolean-returning values and apply a logical operation to them.

I don't understand, with the && and || operators, I want a bool returned, not another type.

Then you don't need to overload anything. Just write an implicit conversion operator from Foo to bool and you can use |, ||, & and && to your heart's content.

Just because you don't want to have a && or || operator that returns something other than bool doesn't mean that no one does. Suppose instead of Foo you wish to write a three valued logic operator where the value can be True, False or Neither. You could define & and | and && and || operators on True, False and Neither. Clearly you would not want the operators to return bool; you want them to return True, False or Neither.

What I don't understand is what the | and & operators have to do with the && and || operators.

They're exactly the same operators. The only difference is that the lazy ones don't evaluate the second operand if doing so is unnecessary.

Why must | and & be overloaded to use || and && though?

See the previous question and answer.

The | and & operators take two operands.

Yes, they do. So do the && and || operators.

Thursday, August 5, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :