What is difference between & and && in java

Questions by khutalnilesh

Showing Answers 1 - 19 of 19 Answers

Rancy

  • Jul 16th, 2006
 

Single Ampercent (& ) can be used as Bitwise operator and Boolean logical operator.

Now the difference arises when & is used as logical operator.

This can be best explained with the help of an example.

int i=5;
if(i<3 & i++ < 10)
{
  //perform action
}

Now in this case
First expression is i<3
Second expression is i++ < 10

when & is used it will evaluate both the expressions regardless of the fact that it finds first expression as FALSE and only then will it give an answer.

Whereas if && was used in place of & , after it had evaluated first expression and had found result of first expression as FALSE, it would not have evaluated second expression. Thus saving time.

FYI... && is also called as counterpart of & and the evaluation it does is called short-circuit evaluation.

I hope this is clear.

  Was this answer useful?  Yes

santosh

  • Jul 20th, 2006
 

single & is used as a boolean operation it means these operation act upon boolean values and there returns a boolean value.it is  called boolean and operator.

ex:Bitwise and

x=10,y=11 then x&y=10

here  00001010----=10

        0001011----11

 

  Was this answer useful?  Yes

santosh

  • Jul 20th, 2006
 

single & is used as a boolean operation it means these operation act upon boolean values and there returns a boolean value.it is  called boolean and operator.

ex:Bitwise and

x=10,y=11 then x&y=10

here  00001010----=10

         0001011----11

 

  Was this answer useful?  Yes

roopa

  • Jul 21st, 2006
 

single & is ampercent, this is a bitwise operator where as double && is an AND operation

  Was this answer useful?  Yes

sravani

  • Jul 26th, 2006
 

actually we mean & is used to represent address and &&means actually and

we can also say & bitwise operator

  Was this answer useful?  Yes

Aayush

  • Aug 7th, 2006
 

& ---- is an amperesent, acts as both bitwise and logical operator.&&---- is a short ckt. and only a logical operator.considered an eg.let's a=null;if(a!=null & a.length())-------------------------// gives a null pointer exception.if(a!=null && a.length())---------------------// not give a null pointer exception.

  Was this answer useful?  Yes

1. & and |, which can also be applied to integral operands for bitwise operations, the conditional operators && and || can only be applied to boolean operands. Their evaluation results in a boolean value.
2.unlike their logical counterparts, there are no compound assignment operators?for?the conditional operators.
3.Conditional operators && and ||, their evaluation is short-circuited.

  Was this answer useful?  Yes

raju g

  • Mar 24th, 2011
 

The Boolean && operator is used for determining two relational expressions and returns true if only if both returns true, otherwise returns false

The Boolean & operator is used to perform Boolean operations on bits of two given arithmetic expressions

For ex: 5 & 6 will return 5. 5 and 6 will be converted into their binary equivalents 101 and 110 respectively. Now the comparison occurs for corresponding place value bits – 1 and 0, 0 and 1 and 1 and 1. This comparison returns 100, i.e., equivalent to 5.

  Was this answer useful?  Yes

It depends on the type of the arguments:-

For integer arguments, the single ampersand ("&")is the "bit-wise AND" operator. The double ampersand ("&&") does not mean anything as it 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, the single ampersand always evaluates both arguments/conditions 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.

  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