2.6.5.2 - Binary Logical Operators

Binary logical operators take two operands and return single boolean value depending the logical operator and the boolean values of the both operands. Binary logical operators are logical AND &, logical exclusive OR ^ and logical OR ||. They applications are covered in this section.

2.6.5.2.1 - Logical AND Operator &

The logical AND operator returns true, only when both of his operands are true, it returns false otherwise. The most important fact about this operator is both operands are always evaluated whatever the result of one of the parties. One explanatory example is given below:

```package preliminary;
public class LogicalAND {
public static void main(String[] args){
boolean controlFalse = false;
boolean controlTrue = true;
System.out.print(" true & true = "  +  controlTrue & controlTrue);
System.out.print(" true & false = "  +  controlTrue & controlFalse);
System.out.print(" false & false = "  +  controlFalse & controlFalse);
} // end main
} //end class LogicalAnd
```

The result of this program :

```        true & true = true
true & false = false
false & false = false

```

As it can be understood from the results of the above program, logical AND returns true only if both of the operands are true. It will return false in all other cases.

2.6.5.2.2 - Logical OR Operator |

The logical OR operator returns true, only when one of his operands is true, it returns false otherwise. An example is given below:

```package preliminary;
public class LogicalOR {
public static void main(String[] args){
boolean controlFalse = false;
boolean controlTrue = true;
System.out.print(" true | true = "  +  (controlTrue | controlTrue));
System.out.print(" true | false = "  +  (controlTrue | controlFalse));
System.out.print(" false | false = "  +  (controlFalse | controlFalse));
} // end main
} //end class LogicalOR
```

The result of this program :

true | true = true
true | false = true
false | false = false

Logical OR returns true only if only one or both of the operands is true. It will return false if both of the operands are false.

2.6.5.2.3 - Logical Exclusive OR (XOR) Operator^

The logical XOR operator returns true, only when one of his operands is the opposite of the other. It returns false otherwise.

```package preliminary;
public class LogicalAND {
public static void main(String[] args){
boolean controlFalse = false;
boolean controlTrue = true;
System.out.print(" true & true = "  +  controlTrue & controlTrue);
System.out.print(" true & false = "  +  controlTrue & controlFalse);
System.out.print(" false & false = "  +  controlFalse & controlFalse);
} // end main
} //end class LogicalAnd
```

The result of this program :

```        true & true = true
true & false = false
false & false = false

```

As it can be understood from the results of the above program, logical AND returns true only if both of the operands are true. It will return false in all other cases.