Boolean Logical Operators in Java

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.
OperatorOperator NameTypeExample
!logical NOTunary!X returns false
&logical ANDbinaryX & Y returns false
|logical ORbinaryX | Y returns true
^logical XOR
(exclusive OR)
binaryX^Y returns true
==Equal tobinaryX == Y returns false
!=Not equal tobinaryX != Y returns true
&&short-circuit ANDbinaryX&&Y returns false
||short-circuit ORbinaryX||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:

XYX | YX & YX ^ Y!X
falsefalsefalsefalsefalsetrue
falsetruetruefalsetruetrue
truefalsetruefalsetruefalse
truetruetruetruefalsefalse

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 .

It produces the output:

 


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. 
OperatorUseEquivalent to
&=X &= YX = X & Y
|=X |= YX = X | Y
^=X ^= YX = X ^ Y
  • Note: != is not a compound form of !, it is a different operator altogether.

It produces the output:

 


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.

It produces the output:

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.