Friday 30 June 2017
Logical operator Vs Bitwise operator in Java : && vs &, || vs |


&& operator is a Logical operator in Java. In Java it's alias names are short-circuit AND operator, and conditional AND operator.
AND Logic Gate
AND Logic Gate
It operates on Boolean variables only (true, false).
In an AND operation involving multiple conditions, if one of the condition is false the rest is not checked, as the result will always be false - This short circuit behavior of the Logical operator is the reason why it is also known as short-circuit operator.

& 
It's called Bitwise operator in Java, as it performs Logical AND operation on bit level.
it is a.k.a Bitwise AND, Unconditional Operator in Java.
It performs AND logical operation on each bit.

Let's observe some important points about && and &.

Compilation error because Operator && is defined only for Boolean values
Compilation error because Operator && is defined only for Boolean values

Note #1: && is defined only for boolean values. It's operands can only be true or false. Else, it produce compilation error.


 Operator & is defined only for Integer and Boolean values
Compilation error because Operator & is defined only for Integer and Boolean values

Note #2: & is defined only for boolean and Integer (i.e., byte, short, char, int and long) values. Else it produce compilation error.



package LogicalVsBitwise;

public class LogicalVsBitwise {
public static void main(String[] args) {
// Logical && Vs Bitwise &
int a = 2;
int b = 9;
System.out.println((a > b) && (++a < b)); //false && -skipped- = false 

System.out.println(a); //-skipped- so a did't get incremented. So, a=2 
System.out.println((a > b) & (++a < b)); //false & true = false 
System.out.println(a); // didn't skip so ++a = 3 

}

Output:-
false
2
false
3

Note #3: && operator skips rest of the conditions if it finds any false condition. Because, the result will be false.

Meanwhile, & operator doesn't skip any condition even if it finds one false condition during the calculation.


package ObesrveIt
public class ObesrveIt {
    public static void main(String[] args) {
        // & operator works only on boolean or int operands
        // Default size of int is 32 bit
        int a = 2;          // 00000000 00000000 00000000 00000010
        int b = 9;          // 00000000 00000000 00000000 00001001
        int result = a & b; // 00000000 00000000 00000000 00000000
        System.out.println(result);
    }
}

Output: 0

Note #4 : AND operation is performed on every bit. Hence, & operator is also known as bit-wise operator in Java.


here
Disadvantage of using & operator
Difference between && and & operator
Note #5:
  • If a String variable stringName is null, executing stringName.length() command throws a java.lang.NullPointerException. 
  • && ignores rest of the condition as soon as it finds a false condition from LEFT-> RIGHT. 
  • & checks for every condition even if it finds a false condition. So, here name.length() command got executed and java.lang.NullPointerException got thrown.


Difference between || and |

OR Logic Gate
OR Logic Gate

Similar to Logical && operator, Logical || operator is known as:
  1. Logical Operator
  2. Conditional Operator and
  3. Logical OR Operator
  • If a Logical OR operation is performed on multiple conditions, || stops executing rest of the conditions as soon as it finds a true condition. Because, the result will be true anyways. - (Opposite to && operator)
  • Just like && operator, || operator is defined for Boolean values only.
Similar to Bit-wise & operator, | operator is known as:
  1. Bit-wise Operator
  2. Unconditional Operator and
  3. Bit-wise OR Operator
  • If a Bit-wise OR operation is performed on multiple conditions, unlike || operator, | operator executes every condition even if it finds a true condition in between.
  • Just like & operator, | operator is defined for boolean and Integer values only. And it performs a Logical OR operation on every bit.


Conclusion:

(Note: There are many other Logical as well as bit operators in Java. But, here we discussed only about &&, &, || and | operator. To read further, check the notes on Bit-Shift operators in Java.)

LOGICAL && and || OPERATOR
  1. Both && and || operator stops executing remaining expression as soon as it finds the main result. i.e.,&& stops execution as soon as it finds a false condition. Whereas, || stops the operation as soon as it find a true condition - This short-circuit like nature gives it the name Short-circuit AND and OR operand.
  2. Both are defined only for Boolean operands.
  3. Both gives out Boolean output only- i.e., true, or false.
  4. It's alias name includes:- Conditional operator, Logical operator, And Short-circuit operator.

BIT-WISE & and | OPERATOR
  1. Unlike Logical operators, Both '&' and '|' operator executes every expression.
  2. These are defined only for boolean and Integer (i.e., byte, short, char, int and long) operands.
  3. & operator performs the AND operation and | operator performs the OR operation on every bit of the input boolean or Integer data types - Hence the name Bit-wise operator.
  4. It's alias name includes:- Unconditional operator, and Bitwise operator.