What is the difference between & and && operators?

Showing Answers 1 - 8 of 8 Answers

rohit singhal

  • Apr 10th, 2006
 

 the && operator is a short circuit operator means if it gets its first operand false it never go to its second operand . but & is not .

for ex:

   if i = 0;

   if(i && i++)

  { System.out.println(i);}

   it will print i = 0  not 1. 

Mohammad Asif

  • Apr 11th, 2006
 

Boolean Logical Operators - & (Logical AND), | (Logical Inclusive OR), ^ (Logical Exclusive OR ) :

Boolean Logical Operators can be applied to Boolean Operands returning a Boolean Value. It can also be applied to integral operands to perform bitwise logical operations. They also have Boolean Logical Compound Assignment Operators i.e. &=, ^=, |=.

Conditional Operators - ( &&, || ) :

They are similar to logical operators but their evaluation is short-circuited. These can be applied only to Boolean Operands ( Not to Integral Operands ). They have no compound assignment operators

  Was this answer useful?  Yes

pranavkohli

  • Aug 11th, 2010
 

It depends on the type of the arguments...

For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") is not defined for anything but two boolean arguments.

For boolean arguments, the single ampersand constitutes the (unconditional) "logical AND" operator while the double ampersand ("&&") is the "conditional logical AND" operator. That is to say that the single ampersand always evaluates both arguments whereas the double ampersand will only evaluate the second argument if the first argument is true.

For all other argument types and combinations, a compile-time error should occur.

sapanindia

  • Aug 13th, 2010
 

These are Bitwise and Logical Operators in Java
1. &= bitwise AND
2. &&= logical AND (Shourt-circuit)

1. These operators & are used with boolean operands and return a boolean value (true or false). These operators evaluate both the operands of an expression

An Ex:-
boolean b1= false;
boolean b2 = true;
b1 & b2= false;

2.The Logical operators && are used when we want to form compound condition by combining two or more relations.
An example is:
a>b && X=10;
a=true;
b=true;
a&&b=true;

Ex if (age>55 && salary<1000);

Difference:-
The & operators evaluate both sides of the operators and then give a boolean
value and  && operator first evaluate the left hand side expression of the operator. If they get their final value in that , They do not the right hand side expression of the operator.

NB:-In an expression, operators that have higher precedence are evaluated before operators that have relatively lower precedence.

Sapan Kumar Das

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions