The Java Logical operators combine two or more conditions and perform the logical operations using && (AND), || (OR), and ! (NOT).
The Relational Operators compare two variables, and what if we want to compare more than one condition? Very simple Java logical operators will do the trick, and the table below describes them with examples.
Java Logical Operators | Name | Description | Example |
---|---|---|---|
&& | AND | It will return true when both conditions are true | If (age > 18 && age <= 25) |
|| | OR | This will return true when at least one of the conditions is true | If (age == 35 || age < 60) |
! | NOT | If the condition is true, the logical NOT operator makes it false | If age = 18 then!( age = 18) returns false. |
For a better understanding, let us see the AND and OR truth tables behind the logical operators in Java.
&& (LOGICAL AND) Truth table.
Condition 1 | Condition 2 | Condition 1 && Condition 2 |
---|---|---|
True | True | True |
True | False | False |
False | True | False |
False | False | False |
|| (LOGICAL OR) Truth table of Java Logical Operators
Condition 1 | Condition 2 | Condition 1 || Condition 2 |
---|---|---|
True | True | True |
True | False | True |
False | True | True |
False | False | False |
Java Logical Operators example
This example will help to know how logical operators in Java Programming are used in If statements. For this example, we are using one integer variable. We used relational and logical operators inside the If Statement to perform a condition check.
package JavaOperators; import java.util.Scanner; public class LogicalOperators { private static Scanner sc; public static void main(String[] args) { int age; sc = new Scanner(System.in); System.out.println(" Please Enter Your Age: "); age = sc.nextInt(); if (!(age > 18)) { System.out.println("You are Too Young"); } else if (age > 18 && age <= 35) { System.out.println("Young Man"); } else if (age == 36 || age <= 60) { System.out.println("You are Middle Age Person"); } else { System.out.println("You are too Old"); } } }
From the Java screenshot below, see that we entered age = 12. It means the age is not greater than 18. So, the first statement was printed.
Let us see what will happen when we change our values to age = 28. It means the age is between 18 and 35. So, the second statement in the Java Logical Operators program is printed.
Please Enter Your Age:
28
Young Man
This time, we entered age= 56. It means the age is between 36 and 60. So, the third statement is printed.
Please Enter Your Age:
56
You are Middle Age Person
We entered age= 65. So, it prints the message in the else block.
Please Enter Your Age:
65
You are too Old
In this example, the first statement will ask the user to enter his / her age. Next, we will assign the user input value to a variable.
The following Java Logical Operators inside the If statement will check whether the age is not greater than 18 (technically, the age is less than 18). If the condition is True, the statement inside the If block is executed.
The Else If condition will check whether the age is greater than 18 and the age is less than or equal to 35. If both these conditions are True, the statement inside the Else If block is executed.
The next else if condition in this Java Logical Operators example checks whether the age is equal to 36 and the age is less than or equal to 60. If both these conditions are True, the statement inside the Else If block is executed.
If everything fails, the statement inside the else block “You are too Old”, is executed.