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
What exactly is a .class file?
How many types of methods are there?
How to make a read-only class in java?
How to perform quicksort in java?
What a static class can contains?
How destructors are defined in java?
What are the different data types in java?
Which collection allows duplicate values in java?
Is it possible to instantiate the abstract class?
Why string is immutable with example?
What are the differences between Java 1.0 and Java 2.0?
How to perform merge sort in java?
How many types of exception can occur in a java program?
What is the implementation of destroy method in java. Is it native or java code?
How do you detect memory leaks?