tags:

views:

2749

answers:

14

Title says it all. It's been discussed for other languages, but I haven't seen it for Java yet.

+2  A: 
| is the binary or operator

|| is the logic or operator
Lucas S.
Missing that | is also a non-short-circuiting boolean operator.
John Meagher
A: 

| = bitwise or, || = logic or

MagicKat
Missing that | is also a non-short-circuiting boolean operator.
John Meagher
A: 

|| is a logical or and | is a bit-wise or.

Steve Moyer
A: 

Java operators

| is bitwise or, || is logical or.

diciu
A: 

Take a look at:

http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html

| is bitwise inclusive OR

|| is logical OR

Jeremy
Missing that | is also a non-short-circuiting boolean operator.
John Meagher
A: 

| is a bitwise operator. || is a logical operator.

One will take two bits and or them.

One will determine truth (this OR that) If this is true or that is true, then the answer is true.

Oh, and dang people answer these questions fast.

scubabbl
+8  A: 

|| is the logical or operator while | is the bitwise or operator.

boolean a = true;
boolean b = false;

if (a || b) {
}

int a = 0x0001;
a = a | 0x0002;
smink
Missing that | is also a non-short-circuiting boolean operator.
John Meagher
@John Meagher: That's implicit, as it's **bitwise**.
Longpoke
+35  A: 

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

Michael Myers
Provide a complete answer and I'll accept it. So far you're the first to pick this aspect of it up.
John Meagher
Missing the bitwise aspect of |
John Meagher
Damn, I'm out of votes today, I'll come back and up-vote this.
John Meagher
Cool, my first accepted answer. Thanks! :D
Michael Myers
+5  A: 

In Addition to the fact that | is a bitwise-operator: || is a short-circuit operator - when one element is false, it will not check the others.

 if(something || someotherthing)
 if(something | someotherthing)

if something is TRUE, || will not evaluate someotherthing, while | will do. If the variables in your if-statements are actually function calls, using || is possibly saving a lot of performance.

Michael Stum
Why would you ever use | in an if statement. || is boolean, | is not, | would only be boolean if your already working on two boolean values.
FlySwat
This is the first answer to get it all.
John Meagher
This answer is incorrect. If something is FALSE, both operators will go on to the next operand. The difference arises only when the first operand is true.
Michael Myers
To bad it has an absolutely ridiculous example.
FlySwat
Michael Stum
John Meagher
Michael Stum
Don't have enough rep to edit other's posts. Thanks for correcting. For the record, I think the example is just fine (if a little simplistic).
John Meagher
Yes, because as Jonathan pointed out: While using | in an if-statement is not used, it is actually one legal usage, although the only "proper "usage *I* can think if would be a huge WTF: if(importantFunction | otherImportantFunction) - to ensure both functions run but only one needs to return true.
Michael Stum
Bill K
A: 

|| returns a boolean value by OR'ing two values (Thats why its known as a LOGICAL or)

IE:

if (A || B)

Would return true if either A or B is true, or false if they are both false.

| is an operator that performs a bitwise operation on two values. To better understand bitwise operations, you can read here:

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

FlySwat
+2  A: 

a | b: evaluate b in any case

a || b: evaluate b only if a evaluates to true

A: 

In Java, a big difference is the type of the result and the values used with it. The logical OR operator | only takes integer types and returns an integer type. This means that it cannot be used as a condition in a if statement without adding a compare operation too.

|| takes only boolean types and returns a boolean, so it cannot be used for bit manipulation.

This type-safety makes it clear when each is appropriate unlike in C where there is no distinct boolean type.

Diastrophism
This part is wrong: "The logical OR operator `|` only takes integer types." Actually `|` also takes boolean operands.
finnw
A: 

if we give if(rem=0) why it throws error

divya
This should be posted as a question, not as an answer on an existing question. You should also try to flesh it out a little more and include your actual code when you post. Thanks!
Michael Myers
Just from what you have posted here, it looks like your problem is that you used one equals sign (`=`) when you needed to use the double equals sign: `==`.
Michael Myers
A: 

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;
}
Peter Lawrey