Logical Operators in Java

Video Tutorial
FREE
Logical Operators thumbnail
This video belongs to
Java Course - Mastering the Fundamentals
12 modules
Certificate
Topics Covered

Logical operators in Java can be defined as operators that help us combine multiple conditional statements. They allow us to combine or invert conditions based on specific requirements, like electronic gates, which combine signals to produce an output. Java has four types of logical operators: AND, OR, NOT, and XOR.

  • AND operator returns true when both conditions under evaluation are true; otherwise, it returns false.
  • The OR operator returns true if any one of the given conditions is true. It returns false if and only if both conditions under evaluation are false.
  • The NOT operator accepts a single value as an input and returns the inverse. Unlike the AND and OR operators, it is a unary operator.

Example of Logical Operators in Java

Output

Logical AND Operator

Syntax:

This operator is called the Logical AND operator. It returns true when both conditions under evaluation are true; otherwise, it returns false.

Short-Circuiting Effect: If the first condition, cond1, evaluates to false, it doesn't check the second condition. It returns false without evaluating the second condition.

We will understand the definition mentioned above with the help of a table that shows the results of the conditions in which this operator has been used. The table represents the use of the AND operator.

Condition1Condition2Condition1 && Condition2
TRUETRUETRUE
FALSETRUEFALSE
TRUEFALSEFALSE
FALSEFALSEFALSE

From the table above, we can see that the AND operator returns true only if both conditions (under consideration) are true. Even if one of the conditions is false, the AND operator returns false.

Example:

Output:

Explanation:

  • In the code above, we find the maximum number out of p, q, r (taken as inputs).
  • If p is the maximum among the p, q, and r variables, then both conditions p > q and p > r should be satisfied. Thus, we have combined these two conditions using the AND (&&) operator.
  • Similar logic is employed for q. If p and q are not maximum, the maximum is obviously r.

Logical OR Operator

Syntax:

This operator is named the Logical OR operator. This operator returns true if any one of the given conditions is true. OR operator returns false if both conditions under evaluation are false.

Short-Circuiting Effect: This operator doesn’t check the second condition if the first one is true. The second condition is checked only and only if the first condition is false.

Let’s see a table for this blog, too.

Condition1Condition2Condition1 OR Condition2
TRUETRUETRUE
FALSETRUETRUE
TRUEFALSETRUE
FALSEFALSEFALSE

The table above clearly shows that the OR operator returns false if and only if both conditions are false. Otherwise, it returns true.

Output

Explanation:

  • The code above takes three numbers representing the sides of a triangle as input and finds out if the triangle is valid or not.
  • To solve this, we have used the property: "Sum of two sides of a triangle is always greater than the third side."
  • Hence, if the sum of any two sides of the input three sides is less than or equal to the third side, the triangle cannot exist.
  • Thus, we have used the OR operator, which returns true if any of the conditions are true.

Logical NOT Operator

Syntax:

This operator is called the Logical NOT operator. It can be used using an exclamation (!) mark. It accepts a single value as an input and returns the inverse of that value. Unlike the AND and OR operators, this is a unary operator.

Let's make a similar truth table for this operator and see what it looks like.

Condition1!Condition1
TRUEFALSE
FALSETRUE

Here, if the condition is true, then the operator returns false, i.e. the opposite of true and vice versa.

Example:

Output

Explanation:

  • In the code above, we are simply printing the output of NOT operator on the two conditions: (p < q) and (p > q).

Logical XOR Operator

Syntax:

This is a bitwise operator and stands for "exclusive or". It performs logical operations as well if the operands are boolean variables.

Condition1Condition2Condition1 XOR Condition2
TRUETRUEFALSE
TRUEFALSETRUE
FALSETRUETRUE
FALSEFALSEFALSE

From the table above, it is clear that when both the inputs are identical, false returns. However, if XOR is performed on opposite operands, true is returned.

Example:

Output

Java Logical Operators

OperatorExampleDescription
Logical ANDcond1 && cond2Returns true only if both cond1 and cond2 are true
Logical ORcond1 || cond2Returns true if atleast one of cond1 and cond2 is true
Logical NOT!condReturns the opposite of input argument cond
Logical XORcond1 ^ cond2Returns true only if cond1 and cond2 are different

Advantages and Disadvantages of Logical Operators in Java

Advantages

  1. Logical operators enhance code readability by succinctly expressing complex conditions, aiding comprehension for developers.
  2. They streamline the debugging process by facilitating the identification of faulty conditions through systematic evaluation.
  3. Logical operators promote code reusability across different program sections, reducing redundancy and enhancing development efficiency.
  4. They enable the creation of flexible code structures adaptable to diverse scenarios, facilitating dynamic responses to program inputs.

Disadvantages

  1. Ambiguity may arise due to the implicit order of evaluation in expressions, necessitating explicit use of parentheses to clarify intent and avoid confusion.
  2. Compared to more complex constructs like if-else and switch-case statements, logical operators offer limited expressive power, hindering the creation of intricate conditionals.
  3. While optimizing code execution, short-circuit evaluation may lead to unintended bugs if the right operands with side effects still need to be executed.
  4. Unexpected behaviour may occur when non-Boolean values are used with logical operators, potentially diverging from the programmer's intended logic.

Conclusion

Logical operators in java are operators that help us combine multiple conditional statements.

  • Logical operators in java help us control the flow of execution of a program.
  • AND operator returns true when both conditions under evaluation are true; otherwise, it returns false.
  • OR operator returns true if any of the given conditions is true. OR operator returns false if both conditions under evaluation are false. The —NOT operator accepts a single value as an input and returns the inverse of that value. Unlike the AND and OR operators, this is a unary operator.