Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Short-circuit logical OR operator (||)

The || operator combines conditions using logical OR. When the left operand is true, it skips the right operand; otherwise, it evaluates it.

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

Syntax

left || right

left || right returns true if at least one operand is true. Operands must have type boolean or Boolean; the result has type boolean.

The left operand is evaluated first. The right is evaluated only when the left is false: this is short-circuit evaluation. A skipped call performs no side effects. In the example, the first expression skips check(), and the second calls it.

For boolean operands, | gives the same logical value but does not skip the right operand. || has lower precedence than &&; use parentheses to make grouping clear.

An evaluated Boolean operand is automatically unboxed. Unboxing null throws NullPointerException; short-circuiting does not protect against unboxing the left operand.

Examples

public class Main {
    static boolean check() {
        System.out.println("check called");
        return false;
    }

    public static void main(String[] args) {
        System.out.println(true || check());
        System.out.println(false || check());
    }
}

Result

true
check called
false

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