Table of contents
1.
Introduction
2.
What are logical operators in Java?
3.
Java Logical Operators List
4.
Logical ‘OR’ Operator (||) 
4.1.
Syntax:
4.2.
Example 
5.
Logical ‘AND’ Operator (&&)
5.1.
Syntax:
5.1.1.
Example 
6.
Logical ‘NOT’ Operator (!)
6.1.
Syntax:
6.2.
Example : 
7.
Logical XOR Operator
7.1.
Syntax
7.2.
Example
7.3.
Java
8.
Advantages of Java Logical Operators
9.
Disadvantages of Java Logical Operators
10.
Frequently Asked Questions
10.1.
What is a logical operator?
10.2.
How to use logical operators in Java?
10.3.
What are boolean operators?
10.4.
What are the three main logical operators?
10.5.
Is null a logical operator?
11.
Conclusion
Last Updated: Jun 6, 2025
Easy

Logical Operators in Java

Author APURV RATHORE
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Operators in Java are special symbols or keywords used to perform operations on variables and values. They help in manipulating data and controlling program flow. Among these, Logical Operators in Java are essential for making decisions based on multiple conditions.

Logical Operators in Java

What are logical operators in Java?

Logical operators in Java are used to perform logical operations on boolean expressions. They help in combining multiple conditions and return true or false based on the logic applied. These operators are commonly used in control flow statements like if, while, and for.

  • AND (&&): Both conditions must be true for the overall expression to be true. If the first condition is false, the second isn't evaluated—this is known as short-circuiting.
  • OR (||): If at least one condition is true, the overall expression is true. Here, short-circuiting occurs if the first condition is true, as the second condition isn’t checked.
  • NOT (!): This operator inverts the truth value of its operand; if the condition is true, NOT makes it false, and vice versa.                                                                                              

Java Logical Operators List

OperatorLogicalDescription
` ` (OR)
&& (AND)LogicalReturns true only if both conditions are true.
! (NOT)LogicalReverses the result of the condition; true becomes false and vice versa.
^ (XOR)LogicalReturns true if only one of the conditions is true, not both.

These Java Logical Operators are essential when working with multiple boolean expressions in conditions. Understanding this java logical operators list helps write better decision-based logic in Java programs.

Logical ‘OR’ Operator (||) 

Logical OR operators, denoted by ‘||,’ return true if at least one of the conditions is true. It returns true if at least one of the conditions is true.

Syntax:

var = condition1 || condition2
You can also try this code with Online Java Compiler
Run Code

Example 

class A {
    public static void main (String[] args) {
        boolean cond1 = true;
        boolean cond2 = false;
        if (cond1 || cond2){
            System.out.println("At least one of the conditions is true");
        }
        else{
            System.out.println("Both of the conditions are false");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

At least one of the conditions is true
You can also try this code with Online Java Compiler
Run Code

 

Explanation:

The variable cond1 is true and variable cond2 is false. As at least one of the two variables is true, the logical OR of them is true, and hence if block is executed and the corresponding statement is printed. 

Let's see a condition table for the logical OR operator:

Condition 1Condition 2Result
TrueTrueTrue
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

The logical OR operator returns True if at least one of the conditions is True. It returns False only when all the conditions are False.

Logical ‘AND’ Operator (&&)

Logical AND operators, denoted by ‘&&’, return true if both of the conditions are true. It returns false if at least one of the conditions is false.

Syntax:

var = condition1 && condition2
You can also try this code with Online Java Compiler
Run Code

Example 

class A {
    public static void main (String[] args) {
        boolean cond1 = true;
        boolean cond2 = false;
        if (cond1 && cond2){
            System.out.println("Both of the conditions are true");
        }
        else{
            System.out.println("At least one of the conditions is false");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

At least one of the conditions is false
You can also try this code with Online Java Compiler
Run Code

 

Explanation:

The variable cond1 is true and variable cond2 is false. As at least one of the two variables is false, the logical AND of them is false, and hence else block is executed and the corresponding statement is printed. 

Condition table for the logical AND operator:

Condition 1Condition 2Result
TrueTrueTrue
TrueFalseFalse
FalseTrueFalse
FalseFalseFalse

Logical ‘NOT’ Operator (!)

Logical NOT operators are denoted by ‘!’. It's a unary operation that returns true if the condition under consideration is either not met or false. In other words, the operator returns true if the condition is false and false if the condition is true.

Syntax:

var = !condition
You can also try this code with Online Java Compiler
Run Code

Example : 

class A {
    public static void main (String[] args) {
        int var1 = 100;
        int var2 = 101;
        if (!(var1>var2)){
            System.out.println("var2 is greater than var1");
        }
        else{
            System.out.println("var1 is greater than var2");
        }
    }
}
You can also try this code with Online Java Compiler
Run Code

 

Output:

var2 is greater than var1
You can also try this code with Online Java Compiler
Run Code

 

Explanation:

The value of var1 is less than var2. Hence var1>var2 is false, and the logical NOT of it is true. Hence the condition of the if statement is satisfied and the corresponding block of the if statement is executed. 

You can practice by yourself on online java compiler.

Logical XOR Operator

The logical XOR (eXclusive OR) operator is a binary operator that returns `True` if exactly one of its operands is `True`, and `False` otherwise. In other words, it returns `True` if the operands have different values, and `False` if they have the same value.

Let's see how it works:
- If both operands are `True`, the XOR operator returns `False`.
- If both operands are `False`, the XOR operator returns `False`.
- If one operand is `True` and the other is `False`, the XOR operator returns `True`.

Syntax

operand1 ^ operand2

Example

  • Java

Java

public class XORExample {
public static void main(String[] args) {
boolean condition1 = true;
boolean condition2 = false;

// Example 1: True ^ True
System.out.println(true ^ true); // Output: false

// Example 2: True ^ False
System.out.println(true ^ false); // Output: true

// Example 3: False ^ True
System.out.println(false ^ true); // Output: true

// Example 4: False ^ False
System.out.println(false ^ false); // Output: false

// Example 5: Using variables
System.out.println(condition1 ^ condition2); // Output: true

// Example 6: Multiple XOR operations
System.out.println(true ^ false ^ true); // Output: false
}
}
You can also try this code with Online Java Compiler
Run Code

Condition table for the logical XOR operator:

Condition 1Condition 2Result
TrueTrueFalse
TrueFalseTrue
FalseTrueTrue
FalseFalseFalse

Advantages of Java Logical Operators

  • Simplifies complex conditions by combining multiple Boolean expressions into a single logical expression.
     
  • Improves code readability in decision-making structures like if, while, and for loops.
     
  • Enhances control flow by allowing precise conditional logic using AND, OR, NOT, and XOR.
     
  • Reduces code redundancy, avoiding repeated checks by grouping logic efficiently.
     
  • Widely supported in all Java versions and easy to learn and implement.

Disadvantages of Java Logical Operators

  • Can become confusing when overused or nested deeply, leading to hard-to-read code.
     
  • Short-circuit behavior in && and || may skip important code if not used carefully.
     
  • Not suitable for non-boolean values, so must ensure operands are boolean expressions.
     
  • Misuse of XOR (^) can lead to unexpected results if the logic is not clearly understood.

Frequently Asked Questions

What is a logical operator?

A logical operator performs boolean logic on one or more conditions, returning true or false based on the combined evaluation.

How to use logical operators in Java?

In Java, logical operators like &&, ||, and ! are used to combine or invert boolean expressions within control statements like if and loops.

What are boolean operators?

Boolean operators are logical operators that perform operations on boolean values (true or false) and return a boolean result.

What are the three main logical operators?

The three main logical operators are AND (&&), OR (||), and NOT (!). They perform logical operations on boolean values.

Is null a logical operator?

No, null is not a logical operator. It is a special value that represents the absence of an object reference.

Conclusion

Logical Operators in Java are fundamental for controlling program flow by combining multiple conditions efficiently. Understanding how to use operators like &&, ||, !, and ^ helps write clear and concise decision-making code. Mastering these Java logical operators improves code readability, performance, and robustness in any Java application.

Recommended Readings:

Live masterclass