I always thought that &&
operator in Java is used for verifying whether both its boolean operands are true
, and the &
oper
&& is a short circuit operator whereas & is a AND operator.
Try this.
String s = null;
boolean b = false & s.isEmpty(); // NullPointerException
boolean sb = false && s.isEmpty(); // sb is false
Besides && and || being short circuiting, also consider operator precedence when mixing the two forms. I think it will not be immediately apparent to everybody that result1 and result2 contain different values.
boolean a = true;
boolean b = false;
boolean c = false;
boolean result1 = a || b && c; //is true; evaluated as a || (b && c)
boolean result2 = a | b && c; //is false; evaluated as (a | b) && c
boolean a, b;
Operation Meaning Note
--------- ------- ----
a && b logical AND short-circuiting
a || b logical OR short-circuiting
a & b boolean logical AND not short-circuiting
a | b boolean logical OR not short-circuiting
a ^ b boolean logical exclusive OR
!a logical NOT
short-circuiting (x != 0) && (1/x > 1) SAFE
not short-circuiting (x != 0) & (1/x > 1) NOT SAFE
I think my answer can be more understandable:
There are two differences between &
and &&
.
If they use as logical AND
&
and &&
can be logical AND
, when the &
or &&
left and right expression result all is true, the whole operation result can be true.
when &
and &&
as logical AND
, there is a difference:
when use &&
as logical AND
, if the left expression result is false, the right expression will not execute.
Take the example :
String str = null;
if(str!=null && !str.equals("")){ // the right expression will not execute
}
If using &
:
String str = null;
if(str!=null & !str.equals("")){ // the right expression will execute, and throw the NullPointerException
}
An other more example:
int x = 0;
int y = 2;
if(x==0 & ++y>2){
System.out.print(“y=”+y); // print is: y=3
}
int x = 0;
int y = 2;
if(x==0 && ++y>2){
System.out.print(“y=”+y); // print is: y=2
}
& can be used as bit operator
&
can be used as Bitwise AND
operator, &&
can not.
The bitwise AND " &" operator produces 1 if and only if both of the bits in its operands are 1. However, if both of the bits are 0 or both of the bits are different then this operator produces 0. To be more precise bitwise AND " &" operator returns 1 if any of the two bits is 1 and it returns 0 if any of the bits is 0.
From the wiki page:
http://www.roseindia.net/java/master-java/java-bitwise-and.shtml
& is a bitwise operator plus used for checking both conditions because sometimes we need to evaluate both condition. But && logical operator go to 2nd condition when first condition give true.
all answers are great
, and it seems that no
more answer is needed
but I just wonted to point out something about &&
operator called dependent condition
In expressions using operator &&, a condition—we’ll call this the dependent condition
—may require another condition to be true for the evaluation of the dependent condition to be meaningful.
In this case, the dependent condition should be placed after the && operator to prevent errors.
Consider the expression (i != 0) && (10 / i == 2)
. The dependent condition (10 / i == 2)
must appear after
the &&
operator to prevent the possibility of division by zero.
another example (myObject != null) && (myObject.getValue() == somevaluse)
and another thing: &&
and ||
are called short-circuit evaluation because the second argument is executed or evaluated only if
the first
argument does not suffice
to determine
the value
of the expression
References: Java™ How To Program (Early Objects), Tenth Edition