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 is a binary operator that functions as either a bitwise AND or a logical (non-short-circuiting) AND, depending on the data types of its operands.

Bitwise AND (Integral Types)

When applied to integral data types (byte, short, int, long, char), the & operator performs a bit-by-bit comparison of the two’s complement binary representation of the operands. Mechanics:
  • For each corresponding bit position, the result bit is 1 if and only if both operand bits are 1. Otherwise, the result bit is 0.
  • Java applies binary numeric promotion before the operation. If neither operand is a long, both are promoted to int. If one is a long, the other is promoted to long.
Syntax:
int a = 12;         // Binary: 0000 1100
int b = 25;         // Binary: 0001 1001
int result = a & b; // Binary: 0000 1000 (Decimal: 8)

Logical AND (Boolean Types)

When applied to boolean operands, the & operator performs a logical AND operation. Mechanics:
  • The expression evaluates to true if and only if both the left-hand side (LHS) and right-hand side (RHS) operands evaluate to true.
  • Non-short-circuiting evaluation: Unlike the conditional AND operator (&&), the & operator guarantees the evaluation of both operands. Even if the LHS evaluates to false (which mathematically guarantees the final result will be false), the RHS expression is still fully executed.
Syntax:
boolean condition1 = false;
boolean condition2 = true;
boolean result = condition1 & condition2; // Evaluates to false

Truth Table

The underlying logic for both contexts maps to the following truth table (where 1 maps to true and 0 maps to false):
Operand 1 (LHS)Operand 2 (RHS)Result (LHS & RHS)
1 / true1 / true1 / true
1 / true0 / false0 / false
0 / false1 / true0 / false
0 / false0 / false0 / false
Master Java with Deep Grasping Methodology!Learn More