Lesson · Java Language Fundamentals
Operators: the full set, and where it bites
12 min readOperators
Every operator family in one pass, then the traps that actually appear in code review: integer division, pre and post increment, short-circuit versus bitwise logic, and masked shift distances.
The families
Operators come in six groups, and only two of them contain real surprises.
- Arithmetic:
+,-,*,/,%. Division truncates for integers, and%keeps the dividend's sign. - Unary:
+x,-x,++x,x++,--x,x--,!b,~bits. Increment has two forms for a reason covered below. - Relational and equality:
<,>,<=,>=,==,!=. For objects,==compares references, which is where wrappers and strings confuse people. - Logical:
&&,||,!. Short-circuit is the defining feature. - Bitwise and shifts:
&,|,^,~,<<,>>,>>>. On integers they operate on bits; on booleans,&and|still work but lose short-circuiting. - Assignment and friends:
=, plus the compound forms+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=,>>>=, each of which hides a cast.
Precedence in one minute
The ladder, top to bottom: postfix, unary, multiplicative, additive, shift, relational, equality, &, ^, |, &&, ||, ternary, assignment. You do not need to recite it. You need to know where it surprises readers.
The famous example is mixing & with ==. Equality binds tighter, so x & 1 == 0 parses as x & (1 == 0), and the compiler catches it because int & boolean is a type error. The shift operators sit surprisingly LOW: a + b << 2 means (a + b) << 2. And assignment associates rightward, so a = b = c assigns c to b, then b to a.
Increment, decrement, and evaluation order
x++ returns the value before the increment; ++x returns the value after. Java also fixes evaluation order left to right, so x++ + x++ is completely defined: the left operand contributes the old value, the right operand the next one, and x lands two steps higher.
The classic puzzle follows from those rules. i = i++; reads the old value through the postfix expression, then the assignment stores that old value back over the incremented one. The net effect is nothing at all; i is unchanged. The fix is not a cleverer arrangement, it is not writing assignment and increment in the same expression.
Short-circuit logic and masked shifts
&& and || stop as soon as the answer is decided, which is what makes s != null && s.length() > 0 safe. The single-character & and | on booleans evaluate BOTH sides, so the same guard written with & throws a NullPointerException. Non-short-circuit boolean logic is occasionally intentional (checking two conditions with side effects), but it should look deliberate in review.
Shifts have a rule that surprises everyone once: the shift distance is masked to the width. For ints only the low 5 bits count, so 1 << 33 is 1 << 1, which is 2. For longs it is the low 6 bits. >> keeps the sign (arithmetic shift), >>> brings in zeros (logical shift), and dividing by a power of two via shift works only for non-negative values: -8 >> 1 is fine, but the pattern breaks with rounding.
& on booleans skips the short-circuit
if (s != null & s.length() > 0) evaluates both sides, so it throws an NPE the moment s is null. The same expression with && is safe because the right side never runs. Whenever you see a single & between conditions, assume it is a typo until proven otherwise.
Interview angles
What is the difference between & and &&?+
Both are AND; && short-circuits and only works on booleans. & is bitwise for integers and non-short-circuiting for booleans: it always evaluates both operands, which changes side effects and null safety. Same story for | and ||.
What does i = i++; do?+
Nothing to i. The postfix expression yields the old value, then the assignment stores that old value back over the incremented one. The language defines this precisely; there is no undefined behaviour, just a result nobody wants. Split it into two statements.
Why does 1 << 33 equal 2?+
Shift distances are masked to the operand width: the low 5 bits for int (33 & 31 = 1) and the low 6 for long. So 1 << 33 is 1 << 1. If you truly need to move a bit 33 places, use a long: 1L << 33.
What is -7 % 3, and why?+
It is -1: Java's % is a remainder, and the result takes the sign of the dividend. Mathematical modulo lives in Math.floorMod, where -7 floorMod 3 is 2. Cyclic indexing from possibly negative values wants floorMod.