What does & do in Java?
Categories:
Understanding the '&' and '&&' Operators in Java

Explore the bitwise AND operator (&
) and the logical AND operator (&&
) in Java, their distinct behaviors, and when to use each for effective programming.
In Java, the ampersand symbol &
and its double form &&
are operators that, while seemingly similar, serve fundamentally different purposes. Understanding their distinctions is crucial for writing correct and efficient Java code. This article will delve into the bitwise AND operator (&
) and the logical AND operator (&&
), explaining their functionality, use cases, and the key differences, particularly regarding short-circuit evaluation.
The Bitwise AND Operator (&
)
The single ampersand &
in Java is the bitwise AND operator. It operates on the individual bits of its operands. When applied to integer types (byte, short, int, long, char), it performs a bit-by-bit comparison. If both corresponding bits are 1, the resulting bit is 1; otherwise, it's 0. When applied to boolean operands, &
acts as a logical AND operator, but it always evaluates both operands, regardless of the first operand's value. This is a key difference from &&
.
public class BitwiseAndExample {
public static void main(String[] args) {
// Bitwise AND with integers
int a = 5; // Binary: 0101
int b = 3; // Binary: 0011
int resultInt = a & b; // Binary: 0001 (Decimal: 1)
System.out.println("Bitwise AND (int): " + resultInt); // Output: 1
// Bitwise AND with booleans (evaluates both sides)
boolean x = true;
boolean y = false;
boolean resultBool = x & y;
System.out.println("Bitwise AND (boolean): " + resultBool); // Output: false
// Demonstrating full evaluation with booleans
int i = 0;
boolean test = (i++ == 0) & (i++ == 1); // Both i++ == 0 and i++ == 1 are evaluated
System.out.println("Value of test: " + test); // Output: true & true -> true
System.out.println("Value of i after bitwise AND: " + i); // Output: 2
}
}
Example demonstrating the bitwise AND operator with integers and booleans.
The Logical AND Operator (&&
)
The double ampersand &&
is the logical AND operator. It is exclusively used with boolean operands and performs a logical conjunction. The most significant characteristic of &&
is its short-circuiting behavior. If the first operand evaluates to false
, the entire expression is known to be false
, and the second operand is not evaluated. This can be crucial for performance and for preventing NullPointerException
s or other side effects.
public class LogicalAndExample {
public static void main(String[] args) {
// Logical AND with booleans (short-circuiting)
boolean p = true;
boolean q = false;
boolean resultLogical = p && q;
System.out.println("Logical AND: " + resultLogical); // Output: false
// Demonstrating short-circuiting
int j = 0;
boolean shortCircuitTest = (j++ == 1) && (j++ == 2); // (j++ == 1) is false, so (j++ == 2) is NOT evaluated
System.out.println("Value of shortCircuitTest: " + shortCircuitTest); // Output: false
System.out.println("Value of j after logical AND: " + j); // Output: 1 (only first j++ executed)
// Common use case: Null check
String myString = null;
if (myString != null && myString.length() > 0) {
System.out.println("String is not null and not empty.");
} else {
System.out.println("String is null or empty."); // This will be printed, no NullPointerException
}
}
}
Example demonstrating the logical AND operator with short-circuiting behavior.
&&
for boolean logic unless you specifically need the side effects or full evaluation of both operands that &
provides. Short-circuiting can prevent errors and improve performance.Key Differences and When to Use Which
The primary distinction between &
and &&
lies in their application and evaluation strategy. &
is versatile, working as a bitwise operator for integers and a non-short-circuiting logical operator for booleans. &&
is strictly a short-circuiting logical operator for booleans. The choice between them depends entirely on the context and desired behavior.
flowchart TD A[Start] B{"Operand 1 (Left) is evaluated"} C{Is Operand 1 False?} D{"Operand 2 (Right) is evaluated"} E{Result is False} F{Result is True} A --> B B --> C subgraph "Logical AND (&&)" C -- Yes --> E C -- No --> D D --> F end subgraph "Bitwise AND (&)" C -- Yes --> D C -- No --> D D --> F end E[Result is False] F[Result is True]
Flowchart illustrating the evaluation difference between &&
(short-circuiting) and &
(full evaluation) for boolean operands.
&
instead of &&
in conditional statements can lead to subtle bugs, especially if the second operand has side effects or can throw an exception (e.g., null.method()
). Be mindful of this distinction.In summary, &
is for bit manipulation or when you explicitly need both sides of a boolean expression to be evaluated. &&
is for standard boolean logic where short-circuiting is desired, which is most of the time. Always consider the implications of side effects and potential NullPointerException
s when choosing between these two operators.