I know the rules for &&
and ||
but what are &
and |
? Please explain these to me with an example.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
While the basic difference is that
&
is used for bitwise operations mostly onlong
,int
orbyte
where it can be used for kind of a mask, the results can differ even if you use it instead of logical&&
.The difference is more noticeable in some scenarios:
First point is quite straightforward, it causes no bugs, but it takes more time. If you have several different checks in one conditional statements, put those that are either cheaper or more likely to fail to the left.
For second point, see this example:
This fails for
null
, as evaluating the second expression produces aNullPointerException
. Logical operator&&
is lazy, if left operand is false, the result is false no matter what right operand is.Example for the third point -- let's say we have an app that uses DB without any triggers or cascades. Before we remove a Building object, we must change a Department object's building to another one. Let's also say the operation status is returned as a boolean (true = success). Then:
This evaluates both expressions and thus performs building removal even if the department update failed for some reason. With
&&
, it works as intended and it stops after first failure.As for
a || b
, it is equivalent of!(!a && !b)
, it stops ifa
is true, no more explanation needed.&& ; || are logical operators.... short circuit
& ; | are boolean logical operators.... Non-short circuit
Moving to differences in execution on expressions. Bitwise operators evaluate both sides irrespective of the result of left hand side. But in the case of evaluating expressions with logical operators, the evaluation of the right hand expression is dependent on the left hand condition.
For Example:
This will print i=26 ; j=25, As the first condition is false the right hand condition is bypassed as the result is false anyways irrespective of the right hand side condition.(short circuit)
But, this will print i=26; j=26,
Those are the bitwise AND and bitwise OR operators.
Thanks to Carlos for pointing out the appropriate section in the Java Language Spec (15.22.1, 15.22.2) regarding the different behaviors of the operator based on its inputs.
Indeed when both inputs are boolean, the operators are considered the Boolean Logical Operators and behave similar to the Conditional-And (
&&
) and Conditional-Or (||
) operators except for the fact that they don't short-circuit so while the following is safe:This is not:
I think you're talking about the logical meaning of both operators, here you have a table-resume:
In Java, the single operators &, |, ^, ! depend on the operands. If both operands are ints, then a bitwise operation is performed. If both are booleans, a "logical" operation is performed.
If both operands mismatch, a compile time error is thrown.
The double operators &&, || behave similarly to their single counterparts, but both operands must be conditional expressions, for example:
if (( a < 0 ) && ( b < 0 )) { ... } or similarly, if (( a < 0 ) || ( b < 0 )) { ... }
source: java programming lang 4th ed
Maybe it can be useful to know that the bitwise AND and bitwise OR operators are always evaluated before conditional AND and conditional OR used in the same expression.