Logical Operators in Java
Video Tutorial
Overview
Logical operators can be defined as a type of operators that help us to combine multiple conditional statements. There are three types of logical operators in Java: AND, OR and NOT operators.
- AND operator returns true when both conditions under evaluation are true, otherwise it returns false.
- OR operator returns true if any one of the given conditions is true. OR operator returns false if and only if both conditions under evaluation are false.
- NOT operator accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.
Scope of Article
The article aims to:
- Discuss the concept the logical operators in Java.
- Illustrate the use of AND, OR, NOT and XOR Operators in Java.
Introduction
You may remember AND, OR and NOT gates from your electronic and tinkering labs.
- AND gate in electrical circuit combined two signals such that the output is on if both signals are on.
- OR gate, on the other hand, combined signals such that the output becomes on if any of the input signals are on.
- NOT gate reverses the input signal. (High input to low output and vice-versa)
and so on. Similar functionalities are performed by the logical operators in Java.
- We can easily use logical operators to combine multiple conditions. Also whenever we tend to apply these operators on two conditions, a logical output is expected.
- We implement them to achieve a boolean output and control the flow of execution in a program.
Let’s now have a quick look at the features of these logical operators in Java.
Features of Logical Operators In Java
- Logical operators help us control the flow of execution of our program.
- We have boolean operators which return only true or false.
- Logical operators can be easily implemented onto single or multiple boolean operands.
- There are three logical operators in Java:
- && (Logical AND)
- || (Logical OR)
- ! (Logical NOT)
Now we have a brief idea about operators, let’s see the detailed explanation and implementation of each logical operator one by one.
AND Operator
Syntax - cond1 && cond2
This operator is named as the Logical AND operator. This operator 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 carry out a check on the second condition. It returns false without evaluating the second condition.
We are going to understand the above-mentioned definition with the help of a table that shows us the results of the conditions in which this operator has been used. The table represents the using of AND operator.
Condition1 | Condition2 | Condition1 && Condition2 |
---|---|---|
TRUE | TRUE | TRUE |
FALSE | TRUE | FALSE |
TRUE | FALSE | FALSE |
FALSE | FALSE | FALSE |
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 are finding the maximum number out of p, q, r (taken as inputs).
- If p is maximum among p, q, and r variables then both condition 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.
OR Operator
Syntax - cond1 || cond2
This operator is named as the Logical OR operator. This operator returns true if any one of the given conditions is true. OR operator returns false if and only 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.
Condition1 | Condition2 | Condition1 OR Condition2 |
---|---|---|
TRUE | TRUE | TRUE |
FALSE | TRUE | TRUE |
TRUE | FALSE | TRUE |
FALSE | FALSE | FALSE |
It is clear from the table above that 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 triangle 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 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 as it returns true if any of the conditions are true.
NOT Operator
Syntax - !
This operator is called as Logical NOT operator. This can be used using an exclamation (!) mark. It accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.
We can make a similar truth table for this operator itself and let’s see how it would look like.
Condition1 | !Condition1 |
---|---|
TRUE | FALSE |
FALSE | TRUE |
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).
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.
Condition1 | Condition2 | Condition1 XOR Condition2 |
---|---|---|
TRUE | TRUE | FALSE |
TRUE | FALSE | TRUE |
FALSE | TRUE | TRUE |
FALSE | FALSE | FALSE |
From the table above, it is clear that when both the inputs are identical, false is returned. However, if XOR is performed on opposite operands, true is returned.
Example:
Output
Logical Operators Example
Output
Logical Operators Table
Operator | Example | Description |
---|---|---|
Conditional AND | cond1 && cond2 | Returns true only if both cond1 and cond2 are true |
Conditional OR | cond1 || cond2 | Returns true if atleast one of cond1 and cond2 is true |
Logical NOT | !cond | Returns the opposite of input argument cond |
Logical XOR | cond1 ^ cond2 | Returns true only if cond1 and cond2 are different |
Summary
- Logical operators can be defined as a type of operators that help us to combine multiple conditional statements.
- There are three types of logical operators in Java:
- AND Operator (&&)
- OR Operator (||)
- NOT Operator (!)
- Logical operators 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 one of the given conditions is true. OR operator returns false if and only if both conditions under evaluation are false.
- NOT operator accepts a single value as an input and returns the inverse of the same. This is a unary operator unlike the AND and OR operators.
- When both the inputs are identical, false is returned by XOR operator. However, if XOR is performed on opposite operands, true is returned.