Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

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).
result = operand1 | operand2;

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
int a = 10;      // Binary: 0000 1010
int b = 6;       // Binary: 0000 0110
int c = a | b;   // Binary: 0000 1110 (Decimal: 14)
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.
boolean a = true;

// The right-hand expression is evaluated even though 'a' is already true.
// This will throw an ArithmeticException due to division by zero.
boolean result = a | (10 / 0 == 0); 
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.
Master Java with Deep Grasping Methodology!Learn More