Skip to main content
The | operator in Java functions as both a bitwise inclusive OR operator for integral types and a logical non-short-circuiting OR operator for boolean types. It evaluates two operands and yields a result based on whether at least one of the corresponding bits or boolean expressions evaluates to a positive state (1 or true).

Bitwise Inclusive OR (Integral Types)

When applied to integral data types (byte, short, int, long, char), the | operator performs a bit-by-bit comparison of the binary representations of the operands. For each corresponding bit position, the operator applies the following truth table:
  • 0 | 0 = 0
  • 0 | 1 = 1
  • 1 | 0 = 1
  • 1 | 1 = 1
Numeric Promotion Rules: Before the bitwise operation occurs, Java applies binary numeric promotion:
  • If either operand is of type long, the other operand is promoted to long, and the result is long.
  • Otherwise, both operands are promoted to int (even if they are byte, short, or char), and the result is int.

Logical Non-Short-Circuiting OR (Boolean Types)

When applied to boolean operands, the | operator evaluates to true if either the left-hand operand or the right-hand operand is true. Crucially, the | operator guarantees the evaluation of both operands. This is the mechanical difference between the bitwise/logical OR (|) and the conditional OR (||). The conditional OR short-circuits (skips evaluating the right-hand operand if the left-hand operand is true), whereas the | operator forces full evaluation.
Return Type: When both operands are boolean, the return type of the | operation is strictly boolean. It cannot be mixed with integral types; attempting to use | with one boolean and one numeric type results in a compilation error.
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More