Wednesday, April 18, 2007

What is the difference between the '&' operator and the '&&' operator?

'&&' is a Logical operator while '&' is a Bitwise operator.
e.g.

int x=12; binary represenation of 12---------> 1100
int y=10; 1010 binary represenation of 10---------> 1010
int z=x & y; binary represenation of (x & y)---------> 1000
Here value of z will be 8.

In case of logical operatior '&&':
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.
if condition1 is true then condition2 is to be evaluated, if it is true then overall result will be true else it will be false.

1 comments :

Anonymous said...

& operator, in case of boolean operands evaluates both LHS and RHS whereas && operator stops if LHS fails the check.

 

Disclaimer
Interview Questions On Java,Java EE Copyright © 2016. Reads: best tracker