Logical operators in java are those operators which are used to form compound conditions by combining two or more conditions or relations. Sometimes in Java, these operators are also called Boolean operators because they return a boolean value.

An example of logical operators is given 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.

Types of Logical Operators in Java


In java, there are three types of logical operators. They are listed in the below table.

Table: Logical Operators

Operators Meaning
1. && AND operator
2. || OR operator
3. ! NOT operator

AND Operator

In AND operator, two expressions (conditions) are combined by && operator. If both conditions are true, the operator returns true. If one expression is false or both expressions are false then the operator returns false.
For example:
      if(x > y && y < z)
           System.out.println("Hello Java");

In the above statement, there are two conditions: x>y and y<z. Since both conditions are joined by the && operator. So if both conditions are true, "Hello Java" will be displayed.

Note that both expressions are evaluated separately and then the && operator compares the result of both.  

Let's take an example program to understand the concept better.
Program source code 1:
    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
Explanations:
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" is not displayed.


2. In the second if statement, both conditions z>y and z<x are true. Therefore, the statement "Java" is displayed.


3. In the third if statement, both conditions are false. Therefore, the statement "Hello Java" is not displayed.

OR Operator

In OR operator, two or more expressions (conditions) are combined by | | (OR) operator. If either one of the conditions is true, the operator returns true. 
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 be displayed. If any of the three conditions are not equal to 1, the message will not be displayed.

Let's see an example program related to OR operator in java.
Program source code 2:
    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
1. In the first if statement, first expression (x == 1) is true. Therefore, the statement "One" is displayed.

2. In the second if statement, two conditions (y == 2) and (z == 5) are correct. Therefore, the output "Two" is displayed.
3. In the third if statement, all conditions are not correct. Therefore, the statement "Three" is not displayed.

Not Operator

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 be displayed. If the (x > y) is not true, the statement "Hello Java" will be displayed.

Let's take an example program related to NOT operator.
Program source code 3:
    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
Explanations: 
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)). 
a. In the first condition, x is not equal to y. Therefore, the NOT operator returns true.
b. In the second condition, (y+5) is greater than z. So, its return type is true.
c. 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.

Final words
Hope that this tutorial has covered almost all important points related to the concepts of the logical operator in java. I hope that you will have understood this topic clearly and enjoyed it. 

If this tutorial is useful and likes it, please share it on the social networking sites for your friends.
Thanks for reading!