Fundamentals 6 min read

Why Does Integer == Fail for 128? Unveiling Java’s Autoboxing Cache

The article explains why comparing two Integer objects with `==` returns true for values within -128 to 127 but false for larger numbers, detailing Java’s integer cache, reference vs. value equality, and the correct use of `.equals()` to avoid subtle bugs.

DevOps Coach
DevOps Coach
DevOps Coach
Why Does Integer == Fail for 128? Unveiling Java’s Autoboxing Cache

Background

When Java developers first encounter autoboxing, they may assume that primitive types and their wrapper objects can be compared directly with ==. This expectation leads to surprising failures when the same code works for small numbers but not for larger ones.

Reference Equality vs. Value Equality

Using == on wrapper objects checks reference equality —whether both variables point to the exact same object in memory—not whether the numeric values are equal.

Integer Cache Mechanism

Java maintains an internal cache of Integer objects for the range -128 to 127. When you assign a value within this range, the JVM reuses the cached instance, so two variables receive the same reference.

Integer a = 127;
Integer b = 127;
System.out.println(a == b); // true

For values outside the cached range, Java creates a new object each time, resulting in distinct references.

Integer c = 128;
Integer d = 128;
System.out.println(c == d); // false

Correct Way to Compare Values

To compare the numeric values of wrapper objects, use the .equals() method, which performs value equality checks.

Integer c = 128;
Integer d = 128;
System.out.println(c.equals(d)); // true

Why the Cache Range Exists

The chosen range of -128 to 127 is based on performance considerations. Small integers are frequently used in loops, array indices, and counters, so caching them reduces memory allocation overhead.

Practical Implications

This behavior is a common interview question and a real‑world source of bugs. Code that relies on == for wrapper comparison may pass unit tests (which often use small numbers) but fail in production when larger values appear.

Key Takeaways

Using == on wrapper classes compares references, not values.

Java caches Integer objects from -128 to 127.

Values outside this range create new objects, causing == to return false.

Always use .equals() for value comparison of wrapper types.

Conclusion

Understanding Java’s integer cache and the distinction between reference and value equality prevents subtle bugs and helps you answer tricky interview questions confidently.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAutoboxingReference EqualityInteger cacheequals method
DevOps Coach
Written by

DevOps Coach

Master DevOps precisely and progressively.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.