Logical Operators
Tags: java, programming, learning, beginners
Published: 2024-05-03 10:44:01

The logical operators **!**, **&&**, **||**, and **^** can be used to create a compound Boolean expression. Sometimes, whether a statement is executed is determined by a combination of several conditions. You can use logical operators to combine these conditions to form a compound Boolean expression. Logical operators, also known as Boolean operators, operate on Boolean values to create a new Boolean value. Here's a list of the Boolean operators.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irndixce0dyf9frxckn4.png)

The not (**!**) operator, which negates **true** to **false** and **false** to **true**. The and (**&&**) operator. The and (**&&**) of two Boolean operands is **true** if and only if both operands are **true**. The or (**||**) operator. The or (**||**) of two Boolean operands is **true** if at least one of the operands is **true**. The exclusive or (**^**) operator. The exclusive or (**^**) of two Boolean operands is **true** if and only if the two operands have different Boolean values.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/akv3madkwt032znfk3fl.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pb4msplujmjnehsh4ftj.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rgqlqjpmdx712c9vwk6e.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08rmuqfu7o5spknjmeab.png)

The program below checks whether a number is divisible by 2 and 3, by 2 or 3, and by 2 or 3 but not both.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i8cup1lyddlfpw1ajkqt.png)

In mathematics, the expression 
`1 <= numberOfDaysInAMonth <= 31` 
is correct. However, it is incorrect in Java, because **1 <= numberOfDaysInAMonth** is evaluated to a **boolean** value, which cannot be compared with **31**. Here, two operands (a **boolean** value and a numeric value) are incompatible. The correct expression in Java is

```
(1 <= numberOfDaysInAMonth) && (numberOfDaysInAMonth <= 31)
```
De Morgan’s law, named after Indian-born British mathematician and logician Augustus De Morgan (1806–1871), can be used to simplify Boolean expressions. The law states:

```
!(condition1 && condition2) is the same as
 !condition1 || !condition2
!(condition1 || condition2) is the same as
 !condition1 && !condition2
```
For example,

```
! (number % 2 == 0 && number % 3 == 0)
```
can be simplified using an equivalent expression:

```
(number % 2 != 0 || number % 3 != 0)
```
As another example,

```
!(number == 2 || number == 3)
```
is better written as

```
number != 2 && number != 3
```
If one of the operands of an **&&** operator is **false**, the expression is **false**; if one of the operands of an **||** operator is **true**, the expression is **true**. Java uses these properties to improve the performance of these operators. When evaluating **p1 && p2**, Java first evaluates **p1** and then, if **p1** is **true**, evaluates **p2**; if **p1** is **false**, it does not evaluate **p2**. When evaluating **p1 || p2**, Java first evaluates **p1** and then, if **p1** is **false**, evaluates **p2**; if **p1** is **true**, it does not evaluate **p2**. In programming language terminology, **&&** and **||** are known as the _short-circuit_ or _lazy_ operators. Java also provides the unconditional AND (**&**) and OR (**|**) operators