The Java Logical operators combine two or more conditions and perform the logical operations using && (Logical AND), || (Logical OR) and ! (Logical NOT).
The Java 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.
The table below describes the Java Logical Operators with examples
OPERATORS | NAME | DESCRIPTION | EXAMPLE |
---|---|---|---|
&& | logical AND | It will return true when both conditions are true | If (age > 18 && age <= 25) |
|| | logical OR | This will returns true when at least one of the conditions is true | If (age == 35 || age < 60) |
! | logical NOT | If the condition is true, logical NOT operator makes it false | If age = 18 then!( age = 18) returns false. |
Let us see the truth tables behind the logical operators in Java for better understanding
&& (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 |
|| (Java LOGICAL OR) Truth table
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 used in If statements.
For this example, we are using one integer variable. Inside the If Statement, we used relational and logical operators in Java to perform condition check.
// Using Java Logical Operators in Else If Statement 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 age is not greater than 18. So, the first statement printed.
Let us see what will happen when we change our values as age = 28. It means age is between 18 and 35. So, the second statement printed.
This time, we entered age = 56. It means age is between 36 and 60. So, the third statement printed.
From the screenshot below, we entered age = 65. So, it prints the message in else block
ANALYSIS
In this Logical Operators example, the following statement will ask the user to enter his / her age. Next, we are going to assign the user input value to a variable.
System.out.println(" Please Enter Your Age: "); age = sc.nextInt();
Else If Condition
Following Java Logical Operators inside the If statement will check whether age is not greater than 18 (technically, age is less than 18 or not). If the condition is True, statement inside the If block executed
if (!(age > 18)) { System.out.println("You are Too Young"); }
The following If statement will check whether age is greater than 18 and age is less than or equal to 35. If both these conditions are True, statement inside the Else If block executed.
else if (age > 18 && age <= 35) { System.out.println("Young Man"); }
If statement in Java Logical Operators example check whether age is equal to 36 and age is less than or equal to 60. If both these conditions are True, statement inside the Else If block executed.
else if (age == 36 || age <= 60) { System.out.println("You are Middle Age Person"); }
If everything fails, then statement inside the else block executed.
System.out.println("You are too Old");