Table of contents
1.
Introduction
2.
What are Logical Operators
3.
Logical ‘OR’ Operator (||) 
3.1.
Syntax:
3.2.
Example :
4.
Logical ‘AND’ Operator (&&)
4.1.
Syntax:
4.1.1.
Example 
5.
Logical ‘NOT’ Operator (!)
5.1.
Syntax:
5.2.
Example : 
6.
Logical XOR Operator
6.1.
Syntax:
6.2.
Example:
6.3.
Java
7.
Frequently Asked Questions
7.1.
What are boolean operators?
7.2.
What are the three main logical operators?
7.3.
Is null a logical operator?
8.
Conclusion
Last Updated: Sep 10, 2024

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

In Java, logical operators evaluate boolean expressions and make decisions based on multiple conditions. The three main logical operators are AND (&&), OR (||), and NOT (!). These operators allow you to combine or negate boolean values, which enable you to create complex conditional statements. In this article, we will learn about the logical operators in Java.

Also see, Duck Number in Java and Hashcode Method in Java.

What are Logical Operators

In Java, logical operators "OR" (||), "AND" (&&), and "NOT" (!) execute operations analogous to digital electronics' AND and OR gates. They are utilized to combine two or more conditions or to invert the evaluation of a condition.

  • 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.                                                                                              

Must Read Shift Registers in Digital Electronics

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. 

Read about Bitwise Operators in C here.

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

Condition 1 Condition 2 Result
True True True
True False True
False True True
False False False

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. 

You can also read about the topic of Java Destructor

Condition table for the logical AND operator:

Condition 1 Condition 2 Result
True True True
True False False
False True False
False False False

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.

Know more about Unary operator overloading in c++ in detail here.

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 1 Condition 2 Result
True True False
True False True
False True True
False False False

Frequently Asked Questions

 

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

In this article, we have extensively discussed logical operators and their implementation in the Java programming language. We hope this blog has helped you enhance your knowledge regarding logical operators and if you want to learn more, check out our articles on Java. Do upvote our blog to help other ninjas grow. 

Live masterclass