Java AND

Java AND Operator is used to perform logical AND operation between two boolean operands.

AND Operator is usually used in creating complex conditions like combining two or more simple conditions.

AND Operator Symbol

The symbol used for AND Operator is &&.

ADVERTISEMENT

Syntax

The syntax to use AND Operator with operands a and b is

a && b

AND Operator supports chaining. For example, the syntax to write a condition with four boolean values is

a && b && c && d

The above expressions returns true if and only if all the operands in that expression are true.

AND Truth Table

The following truth table provides the output of AND operator for different values of operands.

aba && b
truetruetrue
truefalsefalse
falsetruefalse
falsefalsefalse

AND Operation returns true, only if both the operands are true, else, it returns false.

Examples

In the following example, we take two Boolean variables with different combinations of true and false, and find their logical AND output.

Main.java

public class Main {
    public static void main(String[] args) {
        boolean a, b, result;
        
        a = true;
        b = true;
        result = (a && b);
        System.out.println(a + "  && " + b + "  = " + result);
        
        a = true;
        b = false;
        result = (a && b);
        System.out.println(a + "  && " + b + " = " + result);

        a = false;
        b = true;
        result = (a && b);
        System.out.println(a + " && " + b + "  = " + result);

        a = false;
        b = false;
        result = (a && b);
        System.out.println(a + " && " + b + " = " + result);
    }
}

Output

true  && true  = true
true  && false = false
false && true  = false
false && false = false

In the following example, we use logical AND Operator in If Statement’s Condition to combine two simple conditions to form a complex condition.

index.html

public class Main {
    public static void main(String[] args) {
        int a = 4;
        if (a % 2 == 0 && a > 0) {
            System.out.println("true.");
        } else {
            System.out.println("false.");
        }
    }
}

Output

true.

Conclusion

In this Java Tutorial, we learned about Logical AND Operator, it’s syntax, and usage with examples.