Explain the difference between the Boolean & operator and
the && operator?

Answer Posted / ranganathkini

This can be explained with an example

1. The bitwise AND operator ( & )

(boolean expression1) & (boolean expression2)

to evaluate the above expression, Java first evaluates both
boolean expression1 and boolean expression2

hence only if both boolean expression1 and boolean
expression2 evaluate to true, the whole expression evaluates
to true.

2. The conditional AND operator ( && )

( boolean expression1 ) && ( boolean expression2 )

Here Java first evaluates boolean expression1, only if it
evaluates to true, boolean expression2 is evaluated. Hence
boolean expression2 is not evaluated if boolean expression1
evaluates to false.

The conditional AND operator, sometimes called the
short-circuit operator is more efficient that the bitwise
AND operator. As it saves the processing of expression2 by
first evaluating expression1 and ascertaining that the final
result will be false.

Is This Answer Correct ?    25 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why synchronization is important?

589


What is an infinite loop in java? Explain with an example.

588


Are global variables initialized to zero?

511


Explain about fail safe iterators in java?

568


Is arraylist ordered?

563






Do loops java?

556


What methods are used in Servlet?Applet communication?

1667


Which class is used by server applications to obtain a port and listen for client requests?

493


How is string immutable in java?

544


What is a finally block?

568


Explain the private protected method modifier?

548


What's the difference between comparison done by equals method and == operator?

546


Explain different forms of polymorphism?

655


What is a boolean output?

525


Why set do not allow duplicates in java?

581