Java Logical Operators

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

OPERATORSNAMEDESCRIPTIONEXAMPLE
&&logical ANDIt will return true when both conditions are trueIf (age > 18 && age <= 25)
||logical ORThis will returns true when at least one of the conditions is trueIf (age == 35 || age < 60)
!logical NOTIf the condition is true, 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 better understanding

&& (Java LOGICAL AND) Truth table

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

|| (Java 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 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.

Java Logical Operators 1

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.

 Please Enter Your Age: 
28
Young Man

This time,  we entered age = 56. It means age is between 36 and 60. So, the third statement 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 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: 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, 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");