Title says it all. It's been discussed for other languages, but I haven't seen it for Java yet.
Take a look at:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html
| is bitwise inclusive OR
|| is logical OR
| 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.
|| 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;
'|' 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.
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.
|| 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:
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.
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;
}