Logical Operators in Java

The logical operators are AND (&&), OR (||), and Not (!). These operators are used to combine two or more relational expressions. The && and || operators perform Logical-AND and Logical-OR operations on two boolean expressions. These operators exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

These operators act upon operands that are themselves logical expressions and the result is either true or false. In the case of logical AND, the result will be true only if both the operands are true otherwise the result is false. For logical OR, the result will be false if both the arguments are false otherwise the result is true. Logical AND (&&) has higher precedence than logical OR (||). Let a=1, b=2 and c=3. Now let us check the result using the above operators:

Expression Interpretation Value
(a < b) && (b == 2) true true
(a < b) && (c != 3) false false
(a > c) || (b == 2) true true
(c < b) || (a > 3) false false
!(a < b) false false

Program to test logical operators:

class ConditionalDemo1
    public static void main(String[] args)
        int value1 = 1;
        int value2 = 2;
        if ((value1 == 1) && (value2 == 2))
            System.out.println("value1 is 1 AND value2 is 2");
        if((value1 == 1) || (value2 ==1))
            System.out.println("value1 is 1 OR value2 is 1");


Leave a Reply