Quick revision
The short answer
Both getters return Integer, so == compares the two object references, not the quantities. Integer.valueOf returns cached shared instances for -128 to 127, which is why tests with small quantities pass, but larger equal quantities are usually different objects and compare as false. The fix is Objects.equals(a, b), or unboxing to int deliberately after handling null.
What to remember
In this answer, we saw that == between two Integer values compares references, so it only works when both values come from the Integer cache for -128 to 127. That is why tests with small quantities passed while equal quantities of 128 or more compared as false. We fixed it with Objects.equals or with deliberate unboxing after a null check, noted that a half fix with one primitive side can throw a NullPointerException, and suggested using int for a mandatory quantity, static analysis and boundary-value tests.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Both getters return Integer, so == compares the two object references, not the quantities. Integer.valueOf returns cached shared instances for -128 to 127, which is why tests with small quantities pass, but larger equal quantities are usually different objects and compare as false. The fix is Objects.equals(a, b), or unboxing to int deliberately after handling null.
Why this question is asked
This bug is famous because it passes code review and unit tests and then fails in production on the first big order. It is also a good example of why tests with small, convenient values can hide a whole class of defects. Interviewers ask it to see whether we know what == means for objects and why the behaviour changes with the size of the number. In this answer, we will recap reference comparison, reproduce the bug, explain the Integer cache that hides it, and then look at the correct fixes, null handling, and how to stop it from coming back.
1A Quick Recap of == for Objects
For primitive types such as int, == compares values: 5 == 5 is true. For object types, == compares references, which means it asks whether both sides point to the very same object in memory. To compare the contents of two objects, we call equals.
Integer is an object type, a wrapper around one int. So when both getQuantity() methods return Integer, order.getQuantity() == other.getQuantity() does not compare two numbers. It compares two references to Integer objects.
2Reproducing the Bug
We can see the behaviour with a short test that uses a realistic range of quantities.
Notice the cliff between 127 and 128. Nothing about the business logic changes there, which makes the bug very hard to explain from the outside: orders of up to 127 items match, and orders of 128 or more do not.
3Why Small Values Happen to Work
When an int is boxed, for example when a value is passed to new Line(5) or read from a database into an Integer field, Java calls Integer.valueOf. That method keeps a cache of Integer objects for -128 to 127, a range the Java Language Specification requires to be shared. For a value in that range, every call returns the same cached object. So two Integers holding 5 really are the same object, and == happens to give the right answer.
For a value outside the range, Integer.valueOf creates a new object each time. Two quantities of 128 come from two different valueOf calls, so they are two different objects, and == returns false even though equals would return true. Tests written with quantities like 1, 2 and 10 never leave the cache range, which is why they pass.
| Value on both sides | a == b | a.equals(b) |
|---|---|---|
| 5 | true (same cached object) | true |
| 127 | true (same cached object) | true |
| 128 | false (two objects) | true |
| 5000 | false (two objects) | true |
| null and 5 | false | NullPointerException if a is null |
4The Fixes
The right fix depends on whether quantities can be null. If null is possible, Objects.equals is the simplest correct choice, because it handles null on either side. If null is not allowed, unboxing explicitly makes the intent clear and turns a missing value into an immediate, visible error.
There is one more trap to avoid while fixing it. If we change only one side, for example comparing order.getQuantity() == 128 with an int literal, Java unboxes the Integer and compares values. That is correct for non-null values but throws a NullPointerException when the quantity is null. So a half fix can turn a silent wrong answer into a crash.
5Removing the Cause
Often the deeper fix is to ask why the getters return Integer at all. A quantity is rarely optional. If the domain says every order line has a quantity, the field and getter should be int, which makes == correct and removes the null question. Integer is right only when absence is a real, meaningful state, such as a value not yet entered.
We should also make this class of bug hard to write. Static analysis tools flag reference comparison of boxed numbers; SpotBugs has a check for comparing boxed values with ==, and IntelliJ IDEA and SonarQube have similar inspections (check the names in your version). And tests should use boundary values, such as 127, 128 and a large number, rather than only small convenient ones.
6Follow-up questions
Why does the Java Language Specification require the cache?
Because boxing small values is extremely common, and sharing one immutable object for each of them saves allocation. The specification guarantees sharing for -128 to 127 so that code which boxes small values gets predictable behaviour, but it explicitly does not promise anything for larger values.
Could the same bug happen with Long or String?
Yes. Long has the same -128 to 127 cache, so the same cliff appears. With String, == compares references too: two string literals with the same text are usually the same interned object, but strings built at runtime, read from a request or a database, are separate objects, so == gives false while equals gives true.
Would the bug disappear if we raised -XX:AutoBoxCacheMax?
For values below the new limit it would appear to, which is exactly why it is dangerous: the code's behaviour would depend on a JVM flag. The only real fix is comparing values with equals or with primitives.
7Conclusion
In this answer, we saw that == between two Integer values compares references, so it only works when both values come from the Integer cache for -128 to 127. That is why tests with small quantities passed while equal quantities of 128 or more compared as false. We fixed it with Objects.equals or with deliberate unboxing after a null check, noted that a half fix with one primitive side can throw a NullPointerException, and suggested using int for a mandatory quantity, static analysis and boundary-value tests.
Open with
The bug is that both getters return Integer, so == compares references, not values. It asks whether both sides are the same object. The reason tests pass is the Integer cache: autoboxing calls Integer.valueOf, which returns shared cached instances for minus 128 to 127, so two Integers holding 5 really are the same object and == happens to be true.
Then, if they want more
From 128 upwards, valueOf creates a new object each time, so two equal quantities of 128 are different objects and the comparison is false. So everything works for small orders and quietly breaks for larger ones. The fix is to compare values. If the quantity can be null, I use Objects.equals, which handles null on either side.
If it cannot be null, I unbox deliberately, ideally with requireNonNull so a missing value fails loudly, and compare the ints. I would be careful with half fixes: comparing an Integer with an int unboxes it, which is correct but throws a NullPointerException on null.
I would also question why a quantity is an Integer at all; if it is mandatory, an int field removes the problem. And I would add a static analysis rule for boxed reference comparison and use boundary values like 127, 128 and a large number in tests.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A code review finds if (order.getQuantity() == other.getQuantity()) where both getters...": https://scrivio-interview-library.netlify.app/java/java-performance/1e11a4f0e9/