In Java, two separately boxed Integer values can appear equal with == through 127 because Java reuses cached wrapper objects for small values. Above that range, separate objects commonly have different identities, so == can return false even when the numeric values match. For ordinary ID value comparisons, use equals() with a null check, or keep the values as primitive int when object references are unnecessary.
What the 127 boundary means in Java
int is a primitive numeric value; Integer is an object that wraps an int. Assigning an int to an Integer can trigger autoboxing, which converts the value to a wrapper object. The Oracle Press Java SE 8 certification guide describes wrapper caching for values from -128 through 127, including Integer. When separate boxing operations use the cached object for a value in that range, the resulting references can point to the same instance.
As an Amazon Associate I earn from qualifying purchases.
That is why this common example produces the familiar contrast:
Integer a = 127;
Integer b = 127;
System.out.println(a == b); // typically true: cached reference
System.out.println(a.equals(b)); // true: same numeric value
Integer c = 128;
Integer d = 128;
System.out.println(c == d); // commonly false: distinct references
System.out.println(c.equals(d)); // true: same numeric value
The comments describe the usual boxed-value behavior, not a guarantee about every runtime, construction path, or unprovided snippet. The threshold concerns reference reuse; it does not change how numeric equality works. The guide’s range is a Java SE 8 learning reference, and the exact runtime and code that produced any particular result are not established here.
Why `==` can fail for Integer IDs
On two Integer references, == asks whether both variables refer to the very same object. It does not ask whether their wrapped numbers are equal. The cache can make distinct-looking assignments share an object for small values, which can disguise this distinction. Above the cache range, separate boxed values commonly refer to separate objects, making == false even though both contain the same ID.
In short, the ID did not become numerically unequal when it passed 127. The observed change is about object identity, and code that relies on the cache to compare values is fragile.
Rank #2
Choose a comparison that matches the ID type
| Situation | Comparison | What it checks | Null handling |
|---|---|---|---|
Primitive int values |
a == b |
Numeric value equality | Not applicable to primitives |
Non-null Integer references |
a.equals(b) |
Wrapped numeric value equality | Calling on a null a throws; ensure it is non-null first |
Nullable Integer references |
Objects.equals(a, b) |
Null-safe value equality for Integer values |
Handles null arguments; confirm API availability for the project’s Java target |
If an ID is conceptually just a number and does not need to be nullable, primitive int avoids wrapper identity altogether. If the value must be nullable, use a null-safe comparison suited to the project’s Java version. Do not use reference == for ordinary Integer value comparison.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
What if the code is JavaScript?
The 127 explanation is specific to Java’s boxed Integer behavior. JavaScript has different equality rules: == can coerce types, while === avoids that coercion; when comparing objects, strict equality checks identity rather than structural contents. MDN’s JavaScript equality guide does not describe a Java-style Integer cache cutoff at 127. If the code is JavaScript, inspect the actual types and operators rather than applying the Java explanation.
Quick Recap
Best Value
Rank #4
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




