Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Logical AND operator &&

The && operator combines two conditions: the result is true only when both are true. When the left condition is false, the right condition is not evaluated.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

leftCondition && rightCondition

leftCondition && rightCondition returns boolean. Both operands must have type boolean or Boolean; numbers are not allowed. Only two true values produce true.

Java evaluates the left operand first. If it is false, Java skips the right operand, including its side effects. This is short-circuit evaluation. For boolean values, & does not skip the right operand because the left value is false.

In the example, divisor != 0 guards division: zero skips the right side; 3 allows its evaluation. The expected output lines are false and true.

An evaluated Boolean operand is automatically unboxed to boolean. Unboxing null throws NullPointerException; using && alone does not eliminate this risk.

Examples

public class Main {
    public static void main(String[] args) {
        int divisor = 0;
        boolean result = divisor != 0 && 12 / divisor > 2;
        System.out.println(result);

        divisor = 3;
        result = divisor != 0 && 12 / divisor > 2;
        System.out.println(result);
    }
}

Result

false
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources