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

Can a class have an interface?

731


What is tcp and udp?

767


What are abstract methods in java?

862


What is console based application in java?

764


Is it possible to compare various strings with the help of == operator?

733


What is the difference between public, private, protected, and friend access?

763


What is difference between class and object in java?

721


Can we define private and protected modifiers for variables in interfaces?

795


What is keyword in oop?

715


How do you detect memory leaks?

762


Is double bigger than float?

730


Give differences between Quicksort & Mergesort. When should these sorts be used and what is their running time?

754


How to display names of all components in a Container?

2678


What is meant by distributed application? Why are we using that in our application?

744


What does a void function return?

742