Skip to content

Logical operators

Logical operators work with boolean values and return a boolean result.

| Operator | Description | Example | Result | | -------- | -------------------------- | -------- | ------------------------------- | --- | --- | --- | --------------------------- | | && | Logical AND | a && b | true if both true | | | | | Logical OR | a | | b | true if at least one true | | ! | Logical NOT | !a | true if a false | | ^ | Logical XOR (exclusive OR) | a ^ b | true if a and b are different |

Short‑circuit evaluation: && and || evaluate the second operand only if necessary.

int x = 10;
if (x > 5 && (x / 0) > 1) {
// Second part not evaluated because first is false.
}

Examples:

boolean isWeekend = true;
boolean hasWork = false;
if (isWeekend && !hasWork) {
System.out.println("Relax!");
}
if (isWeekend || hasWork) {
System.out.println("Either weekend or work.");
}

XOR: true ^ truefalse; true ^ falsetrue.

Note: There are also non‑short‑circuit versions & and | (bitwise operators) that work with booleans but evaluate both operands.