3

Can any one explain the difference between and usage of OR operator ( || and | ) in java. thanks

e.g:

if(a || b) {
// Do something.
}

and

if(a | b) {
// Do something.
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
AKZap
  • 1,181
  • 6
  • 17
  • 31
  • 1
    the former is logical OR operator, the latter is bitwise OR operator. More or less the main difference is that the logical one evaluate to boolean, the bitwise depends on types being ORed. There're a couple of other differences thou. – BigMike Apr 10 '12 at 07:09

5 Answers5

4

This is simple. http://www.roseindia.net/help/java/o/java-operator.shtml says:

OR operator is a kind of a conditional operators, which is represented by | symbol. It returns either true or false value based on the state of the variables i.e. the operations using conditional operators are performed between the two boolean expressions.

The OR operator (|) is similar to the Conditional-OR operator (||) and returns true, if one or another of its operand is true.

Note: In || operator if have more than one condition and if first condition return true then other conditions ignored but in | operator all condition examin.

There are more information on http://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html

Sam
  • 6,770
  • 7
  • 50
  • 91
3

The first one is a logical or. Both sides of the operator are handled as boolean values and it results in a boolean. In case the variables in question are not boolean themselves they become false if they are 0 or null.

The second one is a bit-wise or operator. This one usually only works with integer numbers. I compares the two values bit by bit and gives the resulting number. For example:

5   | 6   = 7    (decimal)
101 | 110 = 111  (binary)

For further details have a look at Wikipedia: Logical disjunction

Nitram
  • 6,486
  • 2
  • 21
  • 32
  • I completely agree with the post of Nitram. One just has to mention that the Java compiler evaluates `true | false` even if the developer uses the bit-wise operator with boolean values. – Dvin Mar 23 '18 at 14:56
2

If you use the operator || JVM will not bother to evaluate the right-hand operand alone.

Michael
  • 1,152
  • 6
  • 19
  • 36
1

the || operator is a boolean operator

it can be interpreted in simple english as...

if ( a is true or b is true)
{
 //do soemthing
}

the | operator is a logical operator

it works only on integral types like int, char etc...

it is the bitwise OR operation on the two operands

example:

bool a = true;
bool b = false;

bool c = a | b;

//c will  be true


if(a | b )
{


}

is same as

c = a | b;

if ( c == true)
{
  do something;
}
sgarizvi
  • 16,623
  • 9
  • 64
  • 98
1

The first is a logical-or, the latter a bitwise-or. HOWEVER, if the two operators (a and b in your example), are boolean, the bitwise-or is seen as logical-or without short circuiting. This can be be convenient at times.

For example, consider:

boolean getTrue() {
    System.out.println("getTrue() called");
    return true;
}

public static void main(String[] args) {
    boolean a = getTrue() || getTrue();
    System.out.println("Result: " + a);
}

The above will only print "getTrue() called" once as the logical-or (||) can determine the result of the expression immediately, without calling getTrue() a second time. Changing to a bitwise-or (i.e. boolean a = getTrue() | getTrue();) will result in two calls to getTrue().

A similar result will be produced with a bitwise-& operation and a getFalse() method.

Another aspect to keep into consideration is that the bit-wise operators gets preference before logical operators. Therefore, mixing them is not recommended as bitwise-or will be executed before a logical-and, which can cause unwanted behaviour. It can be fixed using brackets (), but I think this should be avoided.

miken32
  • 42,008
  • 16
  • 111
  • 154
Jaco Van Niekerk
  • 4,180
  • 2
  • 21
  • 48