Basic Syntax
Operators
Arithmetic, relational, logical, bitwise, shift, ternary, and assignment operators, detailing precedence and evaluation order.
Interview: Expect questions on short-circuit evaluation, compound assignments casting rules, and bitwise shift edge cases.
Operators are special symbols that perform operations on one, two, or three operands, returning a result. Java defines a rich set of operators, including Arithmetic, Relational, Logical, Bitwise, Shift, and Assignment operators.
Core Idea
Operators manipulate data values. Evaluation order is dictated by operator precedence and associativity rules.
Why It Matters
Understanding short-circuit evaluation prevents bugs (like NullPointerExceptions) when checking object conditions.
Interview Lens
Tests short-circuit side effects, bitwise shifting (unsigned shifts), and compound assignment casting rules.
Operator Classification
1. Short-Circuit Logical Operators
Java uses short-circuiting:
- Logical AND (
&&): If the left expression evaluates to false, the overall result is false, and the right expression is skipped. - Logical OR (
||): If the left expression evaluates to true, the overall result is true, and the right expression is skipped.
This differs from bitwise operators (& and |), which always evaluate both sides of the expression.
2. Shift Operators
- Signed Left Shift (
<<): Shifts bits left, filling empty positions with 0. - Signed Right Shift (
>>): Shifts bits right, keeping the sign bit (copies 0 for positive numbers, 1 for negative numbers). - Unsigned Right Shift (
>>>): Shifts bits right, filling the sign position with 0. Used for binary manipulations where sign is ignored.
3. Precedence and Side Effects
Precedence rules decide evaluation order. Postfix increments (x++) run after assignments, whereas prefix increments (++x) run before assignments.
Common Pitfalls
- Side Effects in Short-Circuit Operations: Writing code like
if (a || b++)is risky. Ifais true, the program skipsb++, leavingbunincremented and causing hard-to-find bugs. - Integer Division by Zero: Performing division with integer variables (e.g.
5 / 0) throws a runtimeArithmeticException. However, floating-point division (e.g.5.0 / 0.0) returnsInfinityinstead of throwing an exception.
Best Practices
- Use Parentheses for Clarity: Even if you know operator precedence, wrap expressions in parentheses (e.g.
(a && b) || c) to make the code easier to read. - Prefer Short-Circuiting: Always use
&&and||for boolean checks (like null pointer validations) to avoid evaluating expressions on null references.
Interview-Relevant Information
Q1: What is the difference between >> and >>> operators?
Answer: >> is the signed right shift operator; it preserves the sign bit (fills empty positions with the sign bit value). >>> is the unsigned right shift operator; it always fills empty positions with 0, regardless of the sign.
Q2: How does a compound assignment operator (like +=) perform casting?
Answer: Compound assignments perform an implicit cast. Writing byte b = 1; b += 2; is equivalent to writing b = (byte)(b + 2);. A normal assignment like b = b + 2; would fail to compile because the result is promoted to an int.
Quick Checklist
Can you explain short-circuit evaluation, compare the signed and unsigned shift operators, and explain how compound assignments perform casting? If yes, you are ready for operator questions.
Use Cases
Validating object references and properties in single-line conditional statements.
Implementing bitwise flags and masks to optimize network protocols.
Common Mistakes
Using logical AND (&) instead of short-circuit AND (&&), leading to NullPointerExceptions.
Relying on state changes (side effects) inside short-circuit evaluations.