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 is java used for?
How do you write a scanner class in java?
What does the “final” keyword mean in front of a variable? A method? A class?
What steps are taken when the OS shifts from one-thread execution to another?
Does .length start 0 java?
What is difference between word and integer?
What is Gang of four design patterns
Can extern variables be initialized?
What are the data types supported by java?
What is the preferred size of a component in java programming?
What is the difference between jvm and jre? What is an interface?
Why is the singleton pattern considered to be an anti pattern?
Which category the java thread do fall in?
What is difference between iterator access and index access?
What are the advantages of assembly language?