if((num1 == num2) || (num1 == num3))
The single bar is the short circuit
if((num1 == num2) | (num1 == num3))
If num1 == num2 then the compiler does not go on to check whether num1 == num3 or not. The result of the whole expression would still be true anyway. Short circuit is more efficient of course but should be used with a lot of care.
Sorry, but you are incorrect. Java is based on C/C++ in many ways and this is one of them. A single | is a bitwise or and is evaluated no matter what, || is a logical or and is short circuited. Using a | as a logical operator on non-Boolean operands will work proably as you expect (assuming 0 is false and non-zero is true), but using a & as a logical operator on non-Boolean operands can cause unexpected results.
-
int a = 1;
-
int b = 2;
-
if (a & b) {
-
// output true
-
}
-
else {
-
// output false
-
}
-
This pseudo-code will output false not true. To prevent this, ensure that your operands for your bitwise operators are Boolean when using them as non-short-circuiting conditionals.
See
this artical for more information.
Short-circuiting conditionals are the norm as it keeps unnecessary code from being run. If you are using non-short-circuiting conditionals, you are an advanced programmer and have some specific reason as to why you want to have the entire expression evaluated when it is unnecessary to do so.
Adrian