Java Logical Operators

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.

OperatorsNameDescriptionExample
&&ANDIt will return true when both conditions are trueIf (age > 18 && age <= 25)
||ORThis will return true when at least one of the conditions is trueIf (age == 35 || age < 60)
!NOTIf the condition is true, the logical NOT operator makes it falseIf age = 18 then!( age = 18) returns false.

Let us see the truth tables behind the logical operators in Java for a better understanding

&& (LOGICAL AND) Truth table

Condition 1Condition 2Condition 1 && Condition 2
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

|| (LOGICAL OR) Truth table

Condition 1Condition 2Condition 1 || Condition 2
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

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. Inside the If Statement, we used relational and logical operators 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.

Java Logical Operators 1

Let us see what will happen when we change our values as 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 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 age is not greater than 18 (technically, age is less than 18 or not). If the condition is True, the statement inside the If block is executed.

The following else If condition will check whether age is greater than 18 and 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 example checks whether age is equal to 36 and 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, then the statement inside the else block “You are too Old” is executed.