DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk2 min

Java Integer IDs: Why `==` Works Through 127—and What to Use Instead

Java’s small-value Integer cache can make `==` appear to compare IDs correctly through 127. Learn why that is object identity, and which comparison to use instead.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
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.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.