--- title: Logical Operators in Java - Scaler Topics description: Learn about logical operators in Java. Scaler Topics explains logical operators and types of logical operators in detail, along with their implementations. Click here to know more. author: Kabir Singh category: Java --- :::section{.main} 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 three types of logical operators: AND, OR and NOT. - 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. ::: :::section{.main} ## Example of Logical Operators in Java ```java public class LogicalOp { public static void main(String args[]) { boolean x = true; boolean y = false; System.out.println("x && y = " + (x && y)); System.out.println("x || y = " + (x || y)); System.out.println("!(x && y) = " + !(x && y)); System.out.println("x ^ y = " + (x ^ y)); } } ``` **Output** ```java x && y = false x || y = true !(x && y) = true x ^ y = true ``` ::: :::section{.main} ## Logical AND Operator **Syntax:** ```java cond1 && cond2 ``` 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. | 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:** ```java import java.util.Scanner; class ANDOperator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); int r = sc.nextInt(); if ((p > q) && (p > r)) { System.out.println("Maximum: " + p); } else if ((q > p) && (q > r)) { System.out.println("Maximum: " + q); } else { System.out.println("Maximum: " + r); } sc.close(); } } ``` **Output:** ```plaintext 10 40 20 Maximum: 40 ``` **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`. ::: :::section{.main} ## Logical OR Operator **Syntax:** ```java cond1 || cond2 ``` 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. | Condition1 | Condition2 | Condition1 OR Condition2 | | :--------: | :--------: | :--------: | | TRUE | TRUE | TRUE | | FALSE | TRUE | TRUE | | TRUE | FALSE | TRUE | | FALSE | FALSE | FALSE | The table above clearly shows that the **OR** operator returns `false` if and only if both conditions are `false`. Otherwise, it returns `true`. ```java import java.util.*; class OROperator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Input the three sides of the triangle: "); int p = sc.nextInt(); int q = sc.nextInt(); int r = sc.nextInt(); sc.close(); if (p + q <= r || p + r <= q || q + r <= p) { System.out.println("Invalid Triangle"); } else { System.out.println("Valid Triangle"); } } } ``` **Output** ```plaintext Input the three sides of the triangle: 4 6 10 Invalid Triangle ``` **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`. ::: :::section{.main} ## Logical NOT Operator **Syntax:** ```java !cond ``` 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 | | :--------: | :--------: | | 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:** ```java import java.util.*; class NotOperator { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int p = sc.nextInt(); int q = sc.nextInt(); sc.close(); // Let's display the variables. System.out.println("p = " + p); System.out.println("q = " + q); // Implementing the NOT operator here. System.out.println("!(p < q) = " + !(p < q)); System.out.println("!(p > q) = " + !(p > q)); } } ``` **Output** ```plaintext 12 10 p = 12 q = 10 !(p < q) = true !(p > q) = false ``` **Explanation:** * In the code above, we are simply printing the output of **NOT** operator on the two conditions: `(p < q)` and `(p > q)`. ::: :::section{.main} ## Logical XOR Operator **Syntax:** ```java cond1 ^ cond2 ``` 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` returns. However, if **XOR** is performed on opposite operands, `true` is returned. **Example:** ```java public class Scaler { public static void main(String[] args) { boolean x = true; boolean y = false; boolean finalresult = x ^ y; System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("x ^ y: " + finalresult); x = true; y = true; finalresult = x ^ y; System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("x ^ y: " + finalresult); x = false; y = true; finalresult = x ^ y; System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("x ^ y: " + finalresult); x = false; y = false; finalresult = x ^ y; System.out.println("x: " + x); System.out.println("y: " + y); System.out.println("x ^ y: " + finalresult); } } ``` **Output** ```plaintext x: true y: false x ^ y: true x: true y: true x ^ y: false x: false y: true x ^ y: true x: false y: false x ^ y: false ``` ::: :::section{.main} ## Java Logical Operators | Operator |Example| Description | | :--------: |:---:| :--------: | | Logical AND |`cond1 && cond2`| Returns `true` only if both `cond1` and `cond2` are `true` | | Logical 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 | ::: :::section{.main} ## 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. ::: :::section{.summary} ## 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. :::