Wednesday, 18 July 2012

What is difference between & and && in java.



Below are following difference between them:


  1. Single & is Ampercent, this is a Bitwise operator where as double && is an AND operation.
  2. & evaluates both sides of the operation and && evaluates the left side of the operation, if it's true, it continues and evaluates the right side.
  3. && is logical AND operator comparing boolean values of operands only. It takes two operands indicating a boolean value and makes a lazy evaluation on them. 
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.

No comments:

Post a Comment