I always thought that &&
operator in Java is used for verifying whether both its boolean operands are true
, and the &
operator is used to do Bit-wise operations on two integer types.
Recently I came to know that &
operator can also be used verify whether both its boolean operands are true
, the only difference is that it checks the RHS operand even if the LHS operand is false.
Is the &
operator in Java internally overloaded? Or is there some other concept behind this?
all answers are
great
, and it seems thatno
more answeris needed
but I just wonted to point out something about&&
operator calleddependent 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)
mustappear 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 evaluatedonly if
thefirst
argument doesnot suffice
todetermine
thevalue
of theexpression
References: Java™ How To Program (Early Objects), Tenth Edition
&&
and||
are called short circuit operators. When they are used, for||
- if the first operand evaluates totrue
, then the rest of the operands are not evaluated. For&&
- if the first operand evaluates tofalse
, the rest of them don't get evaluated at all.so
if (a || (++x > 0))
in this example the variable x won't get incremented if a wastrue
.‘&&’ : - is a Logical AND operator produce a boolean value of true or false based on the logical relationship of its arguments.
For example: - Condition1 && Condition2
If Condition1 is false, then (Condition1 && Condition2) will always be false, that is the reason why this logical operator is also known as Short Circuit Operator because it does not evaluate another condition. If Condition1 is false , then there is no need to evaluate Condtiton2.
If Condition1 is true, then Condition2 is evaluated, if it is true then overall result will be true else it will be false.
‘&’ : - is a Bitwise AND Operator. It produces a one (1) in the output if both the input bits are one. Otherwise it produces zero (0).
For example:-
int a=12; // binary representation of 12 is 1100
int b=6; // binary representation of 6 is 0110
int c=(a & b); // binary representation of (12 & 6) is 0100
The value of c is 4.
for reference , refer this http://techno-terminal.blogspot.in/2015/11/difference-between-operator-and-operator.html
&& is a short circuit operator whereas & is a AND operator.
Try this.
I think my answer can be more understandable:
There are two differences between
&
and&&
.If they use as logical AND
&
and&&
can be logicalAND
, when the&
or&&
left and right expression result all is true, the whole operation result can be true.when
&
and&&
as logicalAND
, there is a difference:when use
&&
as logicalAND
, if the left expression result is false, the right expression will not execute.Take the example :
If using
&
:An other more example:
& can be used as bit operator
&
can be used as BitwiseAND
operator,&&
can not.From the wiki page:
http://www.roseindia.net/java/master-java/java-bitwise-and.shtml