I would like to add something to the Ankit's comment. The &&, ||, & and | can be used to compare logical expressions that result in a boolean, also the &, | and ^ can be used as a bitwise operator. To be a bit more clear, here is the explain:
- && and || are also known as short circuit logic comparators - & and | are also known (used in a logical expression) as log circuit comparators.
This means you can use any of those to compare expressions that results in a boolean, like a == b or "".equals(someString), the difference between the use of the short and long circuits stands on the comparisions thos made, I mean, if you have false && someBooleanExpression which ever expression you have on the right side it will not be evaluated, this is cause with an AND logical operation, if only one of the operands is false, all the expression is false, the AND operation needs all the operands in true to the expression be true, so, the short circuit evaluates the rigth side only if it is possible to have a true expression result (this possibility is only if you have true on the left side), son in this example no matters what is the result on the rigth side, all the expression will be false since you have false in one side, but in case you type: false & someBooleanExpression, the rigth side will be always evaluated
The same is with the ||, the OR logical comparission always need both side in false to the expression be false, if one of the operands is true, all the expression will be true, so the || just evaluate the rigth side if the left side is false, if the left side is true, it doesn't care of the rigth side, however the expression will be true. In the same way, the | operator always evaluate both sides.
Why you need to know this, cause, if in any situation you use short or long circuit operators it can change your result, in example:
The bitwise operator works in bit level of the operands, I mean the and operation will make a logical AND (&) between the bits of the operands, in example, the value 1 has the bits (just show 4 for this example): 0001, the 5 value has the bits: 0101, if you make and AND between these numbers (1 and 5) in bit level, you'll have 0001, the other bitwise operators, both OR (|) and XOR(^) works in the same way.
Hopefully my explanaition is not hard and helps you.
SCJP 5.0<br />Preparing SCWCD
Joined: Jan 02, 2008
I forgot to tell you about the ^ logical operation, it is a long circuit because it always need to evaluate both sides, if both sides are equals, the result is false, if they are differents, the result is true, so: