The boolean logical operators act only on **boolean** values and produce **boolean** results. There are eight of them.

For example purpose, assume two boolean variables X and Y hold values true and false respectively.

Operator | Operator Name | Type | Example |
---|---|---|---|

! | logical NOT | unary | !X returns false |

& | logical AND | binary | X & Y returns false |

| | logical OR | binary | X | Y returns true |

^ | logical XOR (exclusive OR) | binary | X^Y returns true |

== | Equal to | binary | X == Y returns false |

!= | Not equal to | binary | X != Y returns true |

&& | short-circuit AND | binary | X&&Y returns false |

|| | short-circuit OR | binary | X||Y returns true |

Few things to note here are:

- Don’t confuse bitwise
**&**with boolean**&**. They have the same notation but are two different operators. Bitwise**&**takes numeric operands while boolean**&**takes boolean operands. Another way to think would be:**&**is overloaded to work differently with different type of operands. You can learn about operator overloading after you learn Java Classes. Same goes with**|**and**^**. They are overloaded to perform differently with numeric and boolean operands. - Similarly don’t confuse relational
**==**and**!=**with boolean**==**and**!=**. They are different operatos. relational**==**and**!=**can compare any data type but boolean**==**and**!=**can compare only boolean values.

### A Note

If you have background in C/C++ programming then note that:

- In C/C++ numeric and boolean values can be interchangeably used.
**true**is any non-zero value and**false**is zero or null. - But in Java
**true**and**false**are non-numeric values and they donot relate to zero or nonzero values.

However the mathematics of bitwise and boolean **&**, **|**, **^** are similar:

X | Y | X | Y | X & Y | X ^ Y | !X |
---|---|---|---|---|---|

false | false | false | false | false | true |

false | true | true | false | true | true |

true | false | true | false | true | false |

true | true | true | true | false | false |

##### boolean logical NOT (!)

It is a unary operator, i.e. it takes only one operand. It inverts the boolean state of the operand. For example if X=**true**, then **!**X will return **false** and if Y=**false** then **!**Y wiil return **true**.

##### boolean logical AND (&)

It is a binary operator, i.e. it takes two operands. Whenever both the operands are **true**, it returns **true** else it returns **false**. For example if X=**true** and Y=**false** then X**&**Y would return **false**.

##### boolean logical OR (|)

It is a binary operator, i.e. it takes two operands. If any of the two operands is **true**, it returns **true** else it returns **false**. For example if X=**true** and Y=**false** then X**|**Y would return **true**.

##### boolean logical XOR (^)

It is a binary operator, i.e. it takes two operands. If exactly one of the two operands is **true**, it return **true** else it returns **false**. For example if X=**true** and Y=**false** then X**^**Y would return **true**.

##### Equal to (==)

It is a binary operator i.e. it takes two operands. If both the operands evaluate to the same boolean value, it returns **true** else it returns **false**. For example if X=**true** and Y=**false** then X**==**Y would return **false**.

##### Not Equal to (!=)

It is the opposite of **==** operator. It is a binary operator i.e. it takes two operands. If both the operands evaluate to the same boolean value, it returns **false** else it returns **true**. For example if X=**true** and Y=**false** then X**!=**Y would return **true **.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
// Demonstration of boolean logical operators public class Boolean_logical_operators { public static void main(String[] args) { boolean x=true; boolean y=false; System.out.println("!x = " + !x); System.out.println("x & y = " + (x & y)); System.out.println("x | y = " + (x | y)); System.out.println("x ^ y = " + (x ^ y)); System.out.println("x == y = " + (x == y)); System.out.println("x != y = " + (x != y)); System.out.println("\n"); System.out.println("You can even combine these operators:"); System.out.println("(!x) ^ y = " + ((!x) ^ y) ); System.out.println("(x & y)^x = " + ((x & y)^x) ); } } |

It produces the output:

1 2 3 4 5 6 7 8 9 10 11 |
!x = false x & y = false x | y = true x ^ y = true x == y = false x != y = true You can even combine these operators: (!x) ^ y = false (x & y)^x = true |

### The compound Assignment Forms of logical operators

Note that some binary operators(|, ^, &) have compound assignment forms which are just shortcuts and slightly optimised version of their normal forms.Operator | Use | Equivalent to |
---|---|---|

&= | X &= Y | X = X & Y |

|= | X |= Y | X = X | Y |

^= | X ^= Y | X = X ^ Y |

**Note: !=**is not a compound form of**!**, it is a different operator altogether.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// Demonstration of compound assignment boolean logical operators public class Compound_assignment_boolean_logical_operators { public static void main(String[] args) { System.out.println("Compound assignment forms of &, |, ^"); boolean m, n=false; m = true; m &= n; // equivalent to m = m & n System.out.println("After m &= n , m = " + m); m = true; m |= n; // equivalent to m = m | n System.out.println("After m |= n , m = " + m); m = true; m ^= n; // equivalent to m = m ^ n System.out.println("After m ^= n , m = " + m); } } |

It produces the output:

1 2 3 4 |
Compound assignment forms of &, |, ^ After m &= n , m = false After m |= n , m = true After m ^= n , m = true |

### Short-Circuit Logical Operators

**&&** is called short-circuit NOT and **||** is called short-circuit OR. They are called so for a reason:

If the output of an expression can be determined by the first operand alone, then they won’t bother evaluating the second operand.

- In case of
**&&**, if first operand evaluates to**false**, then it won’t bother evaluatng the second operand because no matter what the second operand is, the result is going to be**false**. - Similarly in case of
**||**, if first operand evaluates to**true**, then it won’t bother evaluating the second operand because no matter what the second operand is, the result is going to be**true**.

Take the example below to demonstrate the way short-circuit operators are different from other boolean operators.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
// Demonstration of short-circuit operators public class Short_circuit { public static void main(String[] args) { int x1=0, x2=0; boolean result_for_short_circuit_AND = (x1 != 0) && ((++x1) > 0); boolean result_for_logical_AND = (x2 != 0) & ((++x2) > 0); System.out.println("result for short-circuit AND, when first operand is false : " + result_for_short_circuit_AND); System.out.println("result for logical AND, when first operand is false : " + result_for_logical_AND); System.out.println("x1 = " + x1); System.out.println("x2 = " + x2); int y1=0, y2=0; boolean result_for_short_circuit_OR = (y1 == 0) || ((++y1) > 0); boolean result_for_logical_OR = (y2 == 0) | ((++y2) > 0); System.out.println("result for short-circuit OR, when first operand is true : " + result_for_short_circuit_OR); System.out.println("result for logical OR, when first operand is true : " + result_for_logical_OR); System.out.println("y1 = " + y1); System.out.println("y2 = " + y2); } } |

It produces the output:

1 2 3 4 5 6 7 8 |
result for short-circuit AND, when first operand is false : false result for logical AND, when first operand is false : false x1 = 0 x2 = 1 result for short-circuit OR, when first operand is true : true result for logical OR, when first operand is true : true y1 = 0 y2 = 1 |

Notice a few things here:

- In case of
**&&**, the first operand evaluates to**false**and the result can be obtained even without evaluating the second operand because no matter what the second operand is, the result is going to be**false**. It does so, and the value of x1 is not incremented. - While
**&**operator doesn’t care if the result can be evaluated with only first operand, it evaluates both the operands always and so x2 is incremented.

- Similarly in case of
**||**, the first operand evaluates to**true**and the result can be obtained even without evaluating the second operand because no matter what the second operand is, the result is going to be**true**. It does so, and the value of y1 is not incremented. - While
**|**operator doesn’t care if the result can be evaluated with only first operand, it evaluates both the operands always and so y2 is incremented.