Logical Operators in Java

Logical operators in Java are those operators that are used to form compound conditions by combining two or more conditions or relational expressions.

In simple words, logical operators combine two or more conditions and evaluate the overall result. For example:

  • Checking if a user is eligible for voting.
  • Checking if username and password are correct.
  • Validating multiple conditions in a form.

Logical operators perform logical operations on boolean values. Therefore, these operators are also called boolean operators in Java because they return a boolean value of either true or false after evaluation.

Logical operators combine more than one comparison into a single condition. They are useful when we need to check more than one condition at a time and use the result to make decisions in a program.

A simple example of a logical operator expression is:

x > y && x > z

This type of expression, which combines two or more relational expressions, is called a logical expression or compound relational expression. The result of a logical expression is also either true or false.

Types of Logical (Boolean) Operators in Java


In Java, there are three types of logical operators. We have listed them in the table below.

Table: Logical Operators

OperatorsMeaning
1. &&AND operator
2. ||OR operator
3. !NOT operator

Logical AND Operator in Java


The logical AND operator (&&) combines two expressions (or conditions) into a single condition group. The basic form is:

expression1 && expression2

The Java Virtual Machine (JVM) evaluates each expression separately, and then the logical AND operator determines the final result by comparing the results of those evaluations.

If the conditions on both sides of && operator are true, the logical AND operator returns true. If one or both conditions on either side of the operator are false, the operator returns false. For example:

if(x > y && y < z)
  System.out.println("Hello Java");

In the if statement, there are two conditions:

  • x > y
  • y < z.

Both conditions are joined using the AND (&&) operator. If both conditions are true, the statement “Hello Java” will display. So, we can say that the logical AND operator returns true only when all the expressions (or operands) are true.

Important Note:

  • The JVM evaluates the conditions in a logical AND operation from left to right.
  • If the first condition is false, the second condition is not evaluated because the result will already be false. This behavior is known as short-circuit evaluation.

Valid Examples of Logical AND Operator

Here are some valid examples to understand the logical AND operator.

1. 2 > 1 && 3 < 4; // true.
2. 1 == 1 && 99 >= 98; // true.
3. 1 == 2 && 5 <= 4; // false.

Let’s take a simple example program where we will check multiple conditions using the logical AND operator.

Example 1:

public class LogicalANDExample {
public static void main(String[] args) 
{
  int x = 10, y = 5;
  boolean result1 = (x == 10 && y == 5);
  System.out.println("Result1: " +result1);
 
  boolean result2 = (x == 10 && y > x);
  System.out.println("Result2: " +result2);
 
  boolean result3 = (x < y && y > x);
  System.out.println("Result3: " +result3);
 }
}

Output:

Result1: true
Result2: false
Result3: false

In this example:

  • We have declared two variables named x and y and assigned values to them.
  • Then, we have used the && operator to evaluate different logical expressions and stored the results in boolean variables.
  • After that, we print the results using System.out.println().

Example 2: Use of Logical AND Operator in If-Else Statement

public class LogicalAndExample {
  public static void main(String[] args) {
    int age = 20;
    boolean hasID = true;
    if(age >= 18 && hasID == true) {
        System.out.println("You are allowed to enter.");
    } else {
        System.out.println("Entry denied.");
    }
  }
}

Output:

You are allowed to enter.

In this example:

  • The first condition, age >= 18, evaluates to true.
  • The second condition, hasID == true, also evaluates to true.
  • Since both conditions (true && true) are true, the program prints a message, “You are allowed to enter.”.

Example 3: Use of AND Operator in If Statement

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

In this example:

  • In the first if statement, there are two conditions: x > y and y > z. The first condition, x > y, is true, but the second condition, y > z, is not true. Therefore, the statement “Hello” will not display.
  • In the second if statement, both conditions, z > y and z < x, are true. Therefore, the statement “Java” will display.
  • In the third if statement, both conditions are false. Therefore, the statement “Hello Java” will not display.

Logical OR Operator in Java


The logical OR operator (||) in Java combines two or more expressions or conditions into a single condition group. It uses the double vertical bars (||) as its symbol. The general syntax to combine two conditions using the logical OR operator is:

condition1 || condition2

The logical OR operator returns true if either one or both of the conditions return true. If the conditions on both sides of the operator are false, the logical OR operator returns false. For example:

if(x == 1 || y == 1 || z == 1)
    System.out.println("Hello");

In this example:

  • There are three conditions: x == 1, y == 1, and z == 1, which are combined using the OR || operator.
  • If any one of the variables (x, y, or z) has the value 1, the message “Hello” will be displayed on the console.
  • If all three conditions are false, the message will not display.

Therefore, we can say that the logical OR operator returns true only if at least one expression (or operand) is true.

Important Note:

  • The JVM evaluates the conditions in a logical OR operation from left to right.
  • If the first condition evaluates to true, the second condition does not evaluate. This is because the OR operator returns true if at least one condition is true, so checking the remaining conditions becomes unnecessary.
  • If the first condition is false, the JVM then evaluates the second condition to determine the final result.

Valid Examples of Logical OR Operator

Here are some valid examples to understand the behavior of logical OR operator in Java.

1. (2 == 2) || (3 > 5); // true because the left condition is true.
2. (5 > 18) || (3 != 9 ); // true because the right condition is true.
3. (4 == 4) || (5 < 9); // true because both conditions are true.
4. (4 < 2) || (2 == 1); // false because both conditions are false.
5. (3 != 3) || (3 >= 9); // false because both conditions are false.

Let’s take some important example programs based on the logical OR operator in Java.

Example 4:

public class LogicalOrExample {
  public static void main(String[] args) {
    int marks = 35;
    boolean sportsQuota = true;
    if(marks >= 40 || sportsQuota == true) {
         System.out.println("You are selected.");
    } else {
         System.out.println("You are not selected.");
    }
  }
}

Output:

You are selected.

In this example:

  • The first condition, marks >= 40, evaluates to false, but the second condition, sportsQuota == true, is true.
  • Since one condition is true, the result becomes true.

Example 3: Use of OR Operator in Multiple If Statements

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

In this example:

  • The first expression, x == 1, is true; the message “One” will display on the console.
  • In the second if statement, two conditions, y == 2 and z == 5, are true. Therefore, the message “Two” will display on the console.
  • In the third if statement, all conditions are not correct. Therefore, the message “Three” will not display on the console.

Logical Not Operator in Java


The logical NOT operator (!) in Java reverses the logic state of its operand. In other words, it reverses the result of a boolean expression. If the condition is true, the logical NOT operator returns false. If the condition is false, the operator returns true.

The general syntax to reverse the result of an expression is:

!condition

For example:

if(!x > y)
     System.out.println("Hello Java");

In this example:

  • If the condition, x > y, is true, the statement “Hello Java” will not display.
  • If the condition, x > y is not true, the statement “Hello Java” will display.

Valid Examples of NOT Operator in Java

Let’s take some more examples based on the NOT operator in Java.

1. !(5 == 5); // false because 5 == 5 is true and the NOT operator reverses the result.
2. !false; // true because the NOT operator reverses false to true.
3. !true; // false because the NOT operator makes it false.

Let’s take an example program based on the use of logical NOT operator.

Example 4:

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

In this example:

  • In the first expression !(x + 2 == 1 + 2),
    • 1 + 2 == 1 + 2
    • 3 == 3 → true
    • !true → false
  • In the second expression !(y == z),
    • !(2 == 5)
    • !false → true
  • In the third expression !(z  > x),
    • !(5 > 1)
    • !true → false
  • In the if statement, there are three conditions: !(x == y), ((y + 5) > z), and (!((z – 3) == 0).
  • In the first condition:
    • !(1 == 2)
    • !false → true
  • In the second condition:
    • 2 + 5 = 7
    • 7 > 5 → true
  • In the third condition:
    • z – 3 = 5 – 3 = 2
    • 2 == 0 → false
    • !false → true

Since all three conditions connected by the logical AND operator evaluate to true, the message “Hello” is displayed on the console.


Key Points about Logical Operators in Java

  • Logical operators work with boolean expressions and return a boolean value (either true or false).
  • They are mainly used in decision-making statements, such as if, while, for, and do-while.
  • Java provides three types of logical operators: &&, ||, and !.
  • Logical operators help combine multiple conditions into a single condition group.
  • The logical AND operator (&&) returns true only if both conditions evaluate to true.
  • The logical OR operator (||) returns true if at least one condition is true.
  • The logical NOT operator (!) reverses the result of a Boolean expression.
  • The logical operators && and || support short-circuit evaluation in Java.
DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.