BackendBench
Coffee
Spot the Bug #8

Equal below one hundred, unequal above

A medium Java puzzle in the equality and identity family. One line below contains a real bug. Try to spot it first, then read why it fails and how to fix it.

mediumEquality and identitybuggy line: 3

Highlighted: line 3. Play this one as a puzzle without the answer.

What is wrong

Both parameters are Integer objects, so == compares references. Autoboxing reuses cached objects for values from -128 to 127, which makes small numbers appear equal, while larger values create separate objects and compare false.

The fix

return ordered.equals(shipped);

How to avoid it next time

Never compare wrapper objects with ==. Use equals, or unbox to int when both sides are known to be non-null.

Want the background? Read Autoboxing and wrapper types in the Java concepts library.

About equality and identity

In Java == compares primitive values but only compares references for objects. Strings, boxed numbers and enums behave differently, and a class that overrides equals without hashCode quietly breaks every hash-based collection.

In code review: Whenever you see == between two objects, stop and ask whether identity was really intended. Use equals or Objects.equals, and look for equals and hashCode written as a pair.

More puzzles in this family

๐Ÿž Play today's puzzleAll 132 bugsโ† #7#9 โ†’

Frequently asked questions

What is the bug in "Equal below one hundred, unequal above"?

Both parameters are Integer objects, so == compares references. Autoboxing reuses cached objects for values from -128 to 127, which makes small numbers appear equal, while larger values create separate objects and compare false. The faulty line is line 3: return ordered == shipped;

How do I fix it?

Replace it with: return ordered.equals(shipped);. Never compare wrapper objects with ==. Use equals, or unbox to int when both sides are known to be non-null.

How do I catch equality and identity in code review?

Whenever you see == between two objects, stop and ask whether identity was really intended. Use equals or Objects.equals, and look for equals and hashCode written as a pair.

Is there a new puzzle every day?

Yes. The daily Spot the Bug puzzle picks one of these snippets for each UTC day, with the difficulty set by the weekday. Your streak is stored only in your browser.

More tools you might need