Difference between & and && in Java?
&
is bitwise.&&
is logical.
&
evaluates both sides of the operation.&&
evaluates the left side of the operation, if it's true
, it continues and evaluates the right side.
What is the difference between & and && in Java?
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1)
<-- this means evaluate (x != 0)
then evaluate (1/x > 1)
then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1)
<-- this means evaluate (x != 0)
and only if this is true then evaluate (1/x > 1)
so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1)
.
EDIT:
exprA | exprB
<-- this means evaluate exprA
then evaluate exprB
then do the |
.
exprA || exprB
<-- this means evaluate exprA
and only if this is false
then evaluate exprB
and do the ||
.
Differences in boolean operators: & vs && and | vs ||
Those are the bitwise AND and bitwise OR operators.
int a = 6; // 110
int b = 4; // 100
// Bitwise AND
int c = a & b;
// 110
// & 100
// -----
// 100
// Bitwise OR
int d = a | b;
// 110
// | 100
// -----
// 110
System.out.println(c); // 4
System.out.println(d); // 6
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:
if((a != null) && (a.something == 3)){
}
This is not:
if((a != null) & (a.something == 3)){
}
"Short-circuiting" means the operator does not necessarily examine all conditions. In the above examples, &&
will examine the second condition only when a
is not null
(otherwise the whole statement will return false, and it would be moot to examine following conditions anyway), so the statement of a.something
will not raise an exception, or is considered "safe."
The &
operator always examines every condition in the clause, so in the examples above, a.something
may be evaluated when a
is in fact a null
value, raising an exception.
Why do we usually use || over |? What is the difference?
If you use the ||
and &&
forms, rather than the |
and &
forms of these operators, Java will not bother to evaluate the right-hand operand alone.
It's a matter of if you want to short-circuit the evaluation or not -- most of the time you want to.
A good way to illustrate the benefits of short-circuiting would be to consider the following example.
Boolean b = true;
if(b || foo.timeConsumingCall())
{
//we entered without calling timeConsumingCall()
}
Another benefit, as Jeremy and Peter mentioned, for short-circuiting is the null reference check:
if(string != null && string.isEmpty())
{
//we check for string being null before calling isEmpty()
}
more info
Difference between & and && in Scala?
Both are logical AND operators in Scala. The first form, &
, will evaluate both operands values, for example:
val first = false
val second = true
if (first & second) println("Hello!")
Will evaluate both first
and second
before exiting the if condition, although we know that once a false
appears in a logical AND, that entire expression will already yield false.
This is what &&
is for, and what it does is short-circuit the evaluation, meaning you only ever evaluate first
s value and then exit the conditional.
You can use bitwise AND (&
) to perform bitwise operations on integers, which is similar to most programming languages.
Related Topics
How to Rotate an Image Gradually in Swing
Java Maximum Memory on Windows Xp
Cut Out Image in Shape of Text
Converting Array to List in Java
Using Regex to Generate Strings Rather Than Match Them
Output in a Table Format in Java's System.Out
Left Padding a String with Zeros
How to Sort an Arraylist in Java
Module Error When Running Javafx Media Application
How to Iterate Through the Files in a Directory and It's Sub-Directories in Java
Can't Add Value to the Java Collection with Wildcard Generic Type
Hibernate JPA Sequence (Non-Id)
Do I Need <Class> Elements in Persistence.Xml
How to Perform Mouseover Function in Selenium Webdriver Using Java