Quick revision
The short answer
Both getters return Integer, so == compares two object references instead of two numbers. It only looks correct for values from -128 to 127, which come from the Integer.valueOf cache and are shared objects; production quantities above 127 are separate objects, so equal values compare as unequal and valid orders are rejected. We fix it with Objects.equals or explicit unboxing with deliberate null handling, and we stop the whole class of bug by making static analysis fail the build on reference comparison of wrappers.
What to remember
In this answer, we saw that == between two Integer getters compares references, and only seems to work for values in the -128 to 127 cache, which is why small test numbers passed while larger production quantities were rejected. We fixed the comparison with Objects.equals or explicit unboxing after handling null, preferred primitive return types where null is not valid, and stopped the pattern across the codebase with build-failing static analysis and boundary 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 two object references instead of two numbers. It only looks correct for values from -128 to 127, which come from the Integer.valueOf cache and are shared objects; production quantities above 127 are separate objects, so equal values compare as unequal and valid orders are rejected. We fix it with Objects.equals or explicit unboxing with deliberate null handling, and we stop the whole class of bug by making static analysis fail the build on reference comparison of wrappers.
Why this question is asked
This is a real production bug that has slipped through many code reviews, because the line reads perfectly naturally and the tests pass. It is a good interview question because it combines three skills: knowing what == means for objects, explaining why the tests were green, and thinking beyond the single line to how a team prevents the same mistake everywhere. In this answer, we will recap how == behaves with wrappers, reproduce the failure, see why the tests hid it, fix the line, and then put guards in place across the codebase.
1A Quick Recap of == With Wrapper Types
Integer is an object that wraps an int. When both operands of == are Integer, Java compares references, which means it asks whether both sides are the same object in memory. It does not look at the numbers inside. Only when at least one side is a primitive does Java unbox the wrapper and compare values.
The confusing part is the Integer cache. Boxing an int calls Integer.valueOf, which returns a shared, pre-built object for every value from -128 to 127. So two Integers holding 50 are usually the same object, and == happens to give the right answer. Two Integers holding 500 are usually different objects, and == gives false.
2Reproducing the Failure
We can reproduce it with a test that uses realistic numbers instead of tiny ones.
- Request quantity 250Jackson boxes it:
Integer.valueOf(250), new object A - Database limit 250the driver boxes it: new object B
- order.getQuantity() == limit.getMax()compares A and B as references
- Same object?Yestrue, only for cached values -128 to 127Nofalse: the valid order is rejected
4 steps in order. Play them, or step through them one at a time.
In production, the quantity usually comes from a parsed JSON request and the limit from a database row. Each source boxes its own Integer, so for any value above 127 the two objects are distinct, and the check fails exactly when the numbers are equal. That matches the symptom: valid orders rejected only occasionally, for larger quantities.
3Why the Tests Passed
Unit tests usually use small, friendly numbers such as 1, 5 or 10. All of those fall inside the cache, so both getters return the very same cached object, and == returns true. The tests are not wrong about the logic; they are simply choosing values for which the bug is invisible. This is a good example of why boundary values belong in tests: a test with 127 and one with 128 would have caught it immediately.
4Fixing the Line
We have two correct options. Objects.equals compares values and treats two nulls as equal and one null as unequal. Explicit unboxing compares numbers, but it throws NullPointerException if either value is null, so we use it only when null is impossible or after we have handled null on purpose.
The deeper fix is to ask whether these values should be Integer at all. If a quantity can never be null, the getter should return int, and then == is a plain numeric comparison and the bug cannot exist. Wrappers should appear only where null carries a real meaning, such as an optional column.
5Stopping This Class of Bug Across the Codebase
Fixing one line does not fix the other places where the same pattern hides. The reliable approach is to let tools find it. SpotBugs reports suspicious reference comparisons of boxed values (its RC_REF_COMPARISON pattern), Error Prone has a reference equality check, and IntelliJ IDEA has an inspection for number objects compared with ==. The exact names vary by version, so check the tool you use.
Finding them is only half of it. We make the check part of the build and configure it as an error, so a pull request that introduces the pattern fails before review. For an existing codebase, we run the check once to get the full list, fix those findings, and then switch it to failing mode so the count can only go down. Alongside the tool, a short rule in the team's guidelines helps: prefer primitive types for values that cannot be null, and add tests at 127, 128 and realistic production values.
6Follow-up questions
Why do unit tests with small sample numbers hide this bug?
Because boxing values from -128 to 127 returns shared cached objects, so two Integers built from the same small number are the same object and == returns true. Tests that use 1, 5 or 100 therefore pass. Only values outside the cache, which are common in production, create distinct objects and expose the reference comparison.
Would changing the JVM cache size fix it?
It would hide it for a larger range by raising -XX:AutoBoxCacheMax, but it is not a fix. Any value above the new bound fails again, the behaviour depends on a startup flag, and other wrapper types such as Long do not follow that setting. The only real fix is comparing values.
Is order.getQuantity().equals(limit.getMax()) good enough?
It compares values correctly, but it throws NullPointerException when the quantity is null. Objects.equals avoids that, or we validate null first and then compare primitives, which makes the rule for missing values explicit.
7Conclusion
In this answer, we saw that == between two Integer getters compares references, and only seems to work for values in the -128 to 127 cache, which is why small test numbers passed while larger production quantities were rejected. We fixed the comparison with Objects.equals or explicit unboxing after handling null, preferred primitive return types where null is not valid, and stopped the pattern across the codebase with build-failing static analysis and boundary tests.
Open with
The bug is that both getters return Integer, so == compares references, not values. Boxing goes through Integer.valueOf, which returns cached shared objects only from minus 128 to 127.
Then, if they want more
The tests used small quantities, so both sides were the same cached object and == was true. In production a quantity of, say, 250 comes from the request and the limit from the database, each boxed separately into its own object, so == returns false even though the numbers are equal, and the order is rejected.
To fix the line I would use Objects.equals, which compares values and handles null, or decide explicitly what a null quantity or limit means, reject it, and then compare intValue results. Better still, if these values can never be null, the getters should return int, and then the comparison is numeric by construction.
To stop it across the codebase I would not rely on reviewers. I would enable a static analysis check for reference comparison of boxed numbers, such as SpotBugs RC_REF_COMPARISON, the Error Prone reference equality check or the IntelliJ inspection, fix the existing findings, and make the check fail the build so it cannot come back.
I would also add boundary tests at 127 and 128 for comparisons like this.
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.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
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 shows if (order.getQuantity() == limit.getMax()) where both getters return...": https://scrivio-interview-library.netlify.app/java/types-and-values/622ab77ba7/