Logical Operators in Java | Boolean, Example

Logical operators in Java are those operators which are used to form compound conditions by combining two or more conditions or relations.

These operators are also called Boolean operators in Java because they return a boolean value of either true or false after evaluation.

Java logical operators combine the more than one comparison into one condition group. It is useful when we to check more than one condition at a time and want to use the outcome.

A simple example of logical operators is as below:

`x > y && x > z`

This kind of expression that combines two or more relational expressions is called logical expression or compound relational expression. The result of logical expression is also a value of true or false.

With the help of boolean or logical operators, we can make a powerful data testing statement by combining logical operators with relational operators and can perform logical operations.

Types of Logical (Boolean) Operators in Java

In Java, there are three types of logical operators. We have listed them in the below table.

Table: Logical Operators

OperatorsMeaning
1. &&AND operator
2. ||OR operator
3. !NOT operator

Logical AND Operator in Java

The AND operator combines two expressions (or conditions) together into one condition group. Both expressions are tested separately by JVM and then && operator compares the result of both.

If the conditions on both sides of && operator are true, the logical && operator returns true. If one or both conditions on either side of the operator are false, then the operator returns false.

For example:

```if(x > y && y < z)
System.out.println("Hello Java");```

In the if statement, there are two conditions: x > y and y < z. Both conditions are joined by the AND (&&) operator. If both conditions are true, “Hello Java” will display.

JVM evaluates both expressions separately and then && operator compares the result of both.

So, we can say that the AND operator returns true only if all the expressions (or operands) are true. Here are some more examples to understand logical AND operator.

```1. 2 > 1 && 3 < 4; // true.
2. 1 == 1 && 99 >= 98; // true.
3. 1 == 2 && 5 <= 4; // false.
```

Let’s create a program where we will implement && operator to combine two conditions.

Program code 1:

```package javaProgram;
public class LogicalANDExample {
public static void main(String[] args)
{
int x = 10, y = 5;
boolean result1 = (x == 10 && y == 5);
System.out.println("Result1: " +result1);
boolean result2 = (x == 10 && y > x);
System.out.println("Result2: " +result2);
boolean result3 = (x < y && y > x);
System.out.println("Result3: " +result3);
}
}
```
```Output:
Result1: true
Result2: false
Result3: false
```

Program code 2:

```package logicalOperatorPrograms;
public class LogicalOperatorDemo {
public static void main(String[] args)
{
int x = 200;
int y = 50;
int z = 100;
if(x > y && y > z)
{
System.out.println("Hello");
}
if(z > y && z < x)
{
System.out.println("Java");
}
if((y+200) < x && (y+150) < z)
{
System.out.println("Hello Java");
}
}
}```
```Output:
Java```

Explanation:

1. In the first if statement, there are two conditions: x > y and y > z. The condition x > y is true, but y > z is not true. Therefore, the statement “Hello” will not display on the console.

2. In the second if statement, both conditions z > y and z < x are true. Therefore, the statement “Java” will display on the console.

3. In the third if statement, both conditions are false. Therefore, the statement “Hello Java” will not display on the console.

OR Operator in Java

The logical OR operator in Java combines two or more expressions or conditions together into a single condition group. It uses the double pipes (||) as a symbol.

The OR operator returns true if either one or both of the conditions returns true. If the conditions on both sides of the operator are false, the logical OR operator returns false.

For example:

```if(x = 1 || y = 1 || z = 1)
System.out.println("Hello");```

In the above example, there are three conditions: x = 1, y = 1, and z = 1 which are combined by || (or operator).

If either of x or y or y value becomes equal to 1, then the next statement “Hello” will display on the console. If any of the three conditions are not equal to 1, the message will not display.

So, we can say that the logical OR operator returns true only if at least one expression (or operand) is true. Here are some more examples to understand logical OR operator.

```1. (2 == 2) || (3 > 5); // It will return true because comparison on the left side is true.
2. (5 > 18) || (3 != 9 ); // It will return true because comparison on the right side is true.
3. (4 == 4) || (5 < 9); // It will return true because comparison on both sides is true.
4. (4 < 2) || (2 == 1); // It will return false because both comparisons are false.
5. (3 != 3) || (3 >= 9); // It will return false because both comparisons are true.
```

Let’s see an example program based on the OR operator in java.

Program code 3:

```package logicalOperatorPrograms;
public class OROperatorExample {
public static void main(String[] args)
{
int x = 1;
int y = 2;
int z = 5;
System.out.println("x: " +(x==1));
System.out.println("y: " +(y==z));
System.out.println("z>x: " +(z>x));
if(x==1 || x>y || x>z)
{
System.out.println("One");
}
if(x==y || y==2 || z==5)
{
System.out.println("Two");
}
if(x==y || y==z || z==x)
{
System.out.println("Three");
}
} }```
```Output:
x: true
y: false
z>x: true
One
Two```

Explanation:

1. In the first if statement, first expression (x == 1) is true. Therefore, the statement “One” will display on the console.

2. In the second if statement, two conditions (y == 2) and (z == 5) are correct. Therefore, the output “Two” will display.

3. In the third if statement, all conditions are not correct. Therefore, the statement “Three” will not display on the console.

Not Operator in Java

The NOT operator is used to reverse the logic state of its operand. If the condition is correct, the logical NOT operator returns false. If the condition is false, the operator returns true.

For example:

```if(!( x > y ))
System.out.println("Hello Java");```

In the above example, if the condition (x > y) is true, the statement “Hello Java” will not display. If the (x > y) is not true, the statement “Hello Java” will display.

Let’s understand the NOT operator with the help of some more examples.

```1. !(5 == 5); // It will return false because 5 is equal to 5 is true, but NOT operator makes this statement false.
2. !false; // It will return true because NOT operator reverses the false into true.
3. !true; // It will return false because NOT operator makes it false.
```

Let’s take an example program related to NOT operator.

Program code 4:

```package logicalOperatorPrograms;
public class NotOperatorExample {
public static void main(String[] args)
{
int x = 1;
int y = 2;
int z = 5;
System.out.println("x: " +(!((x+2)==(1+2))));
System.out.println("y: " +(!(y==z)));
System.out.println("z>x: " +(!(z > x)));

if(!(x==y) && ((y+5) > z) && (!((z-3)==0)))
{
System.out.println("Hello");
}
}
}```
```Output:
x: false
y: true
z>x: false
Hello```

Explanation:

1. In the expression ((x + 2)==(1 + 2)), (x + 2) is equal to (1 + 2). Therefore, NOT operator returns false.

2. In the expression (y==z), y is not equal to z. Therefore, the output is true.

3. In the expression (z > x), z is greater than x. Therefore, the output is false.

4. In the if statement, there are three conditions: (!(x==y)), ((y + 5) > z), and (!((z – 3)==0)).

• In the first condition, x is not equal to y. Therefore, the NOT operator returns true.
• In the second condition, (y + 5) is greater than z. So, its return type is true.
• In the third condition, (z – 3) is not equal to 0. Therefore, the NOT operator returns true.

Since all three conditions are true for AND operator. Therefore, the statement “Hello” is displayed.

Hope that this tutorial has covered all important points related to logical operators in java. I hope that you will have understood this topic clearly and enjoyed it.

If this tutorial is useful, please share it on social networking sites for your friends.