Java Integer == Fails at 128 — The Cache Boundary Bug
Integer comparison returns false for 200 but true for 50.
20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.
- ✓Solid grasp of fundamentals
- ✓Comfortable reading code examples
- ✓Basic production concepts
- Autoboxing = compiler automatically converts primitive to wrapper (int → Integer) via Integer.valueOf()
- Unboxing = compiler automatically converts wrapper to primitive (Integer → int) via intValue()
- Integer cache: values -128 to 127 return cached objects — == works for 100, fails for 200
- Performance: Using Long in a loop instead of long creates heap objects each iteration = 5-10x slower + GC pressure
- Production trap: Unboxing null Integer → NullPointerException on hidden intValue() call, stack trace points to innocent-looking arithmetic
- Biggest mistake: Using == to compare Integer objects — it works for small numbers (cache) and fails for large numbers (new objects), making the bug intermittent
Autoboxing and unboxing are Java's automatic conversions between primitive types (int, double, boolean) and their wrapper classes (Integer, Double, Boolean). Introduced in Java 5, this feature exists to bridge the gap between Java's object-oriented collections (which only store objects) and its performance-critical primitives.
When you write Integer x = 42, the compiler silently inserts a call to Integer.valueOf(42) — and that's where the trouble starts. The Integer.valueOf() method caches values from -128 to 127 by default, meaning Integer a = 100; Integer b = 100; a == b returns true, but Integer c = 128; Integer d = 128; c == d returns false.
This cache boundary bug has burned countless developers who assume == compares values rather than object references. The fix is simple: always use .equals() for wrapper comparisons, or better yet, stick to primitives where possible. The performance implications are equally brutal — autoboxing in tight loops creates unnecessary object allocations that can tank throughput by orders of magnitude, and unboxing a null reference throws a NullPointerException that's notoriously hard to trace.
Understanding this mechanism is essential for anyone writing Java that touches collections, generics, or any code where primitives and objects mix.
Imagine you have a coin (a primitive int) and a coin purse (an Integer object). Sometimes a shop only accepts purses, not loose coins. Autoboxing is Java automatically dropping your coin into a purse before handing it over. Unboxing is Java taking the coin back out when you need raw math. You never see it happen — Java just does it quietly behind the scenes. The catch? Opening and closing purses costs a tiny bit of effort, and if the purse is empty (null), handing it over causes a crash.
Every Java developer writes code that mixes primitive types and their object counterparts constantly — passing an int into an ArrayList, returning an Integer from a method, or comparing values with ==. For years before Java 5, you had to manually convert between them, writing verbose boilerplate that cluttered your logic and made bugs easier to hide. Autoboxing changed that, and understanding it deeply separates developers who write clean, performant code from those who wonder why their app slows down or throws a NullPointerException out of nowhere.
The problem autoboxing solves is the fundamental tension at the heart of Java: primitives (int, double, boolean, etc.) are fast and live on the stack, but the Collections framework and generics only work with objects. You can't put an int into a List<Integer> directly — the JVM needs a full-fledged object with a memory address. Autoboxing bridges that gap automatically, letting the compiler handle the conversion so your code stays readable without you thinking about it every single time.
After reading this article you'll understand exactly what autoboxing and unboxing are, why they exist, how the JVM handles the conversion under the hood (including the integer cache you've probably never heard of), and the three real mistakes that trip up even experienced developers. You'll also walk away with solid answers to the interview questions that actually get asked about this topic.
What Autoboxing and Unboxing Actually Are (Under the Hood)
Java has eight primitive types: byte, short, int, long, float, double, char, and boolean. Each one has a corresponding wrapper class in java.lang — Integer, Double, Boolean, etc. These wrapper classes turn a primitive into a full Java object, which means it can be stored in collections, used with generics, passed where Object is expected, and set to null.
Autoboxing is the compiler automatically calling Integer.valueOf(int) for you when a primitive is used where an object is expected. Unboxing is the compiler automatically calling intValue() (or the equivalent) when an object is used where a primitive is expected. This all happens at compile time — the compiler inserts the conversion calls into the bytecode so the JVM never sees the raw conversion gap.
This isn't magic and it isn't free. Every autoboxed value allocates a new object on the heap (with one important exception we'll cover). Knowing that the compiler is secretly inserting method calls lets you predict performance, understand NullPointerExceptions, and reason about == comparisons that behave in surprising ways.
package io.thecodeforge.java; public class AutoboxingBasics { public static void main(String[] args) { // --- AUTOBOXING --- // The compiler rewrites this line as: Integer boxedScore = Integer.valueOf(42); // You write a primitive literal, Java wraps it into an Integer object. Integer boxedScore = 42; // --- UNBOXING --- // The compiler rewrites this as: int rawScore = boxedScore.intValue(); // You use an Integer where a primitive is needed, Java unwraps it. int rawScore = boxedScore; // --- AUTOBOXING IN A COLLECTION --- // ArrayList only holds objects, not primitives. // Each add() call autoboxes the int literal into an Integer object. java.util.List<Integer> playerScores = new java.util.ArrayList<>(); playerScores.add(100); // compiler inserts: Integer.valueOf(100) playerScores.add(250); // compiler inserts: Integer.valueOf(250) playerScores.add(75); // compiler inserts: Integer.valueOf(75) // --- UNBOXING IN ARITHMETIC --- // get() returns an Integer object, but + operator needs primitives. // The compiler inserts .intValue() calls on both operands automatically. int totalScore = playerScores.get(0) + playerScores.get(1); System.out.println("Boxed score (Integer object): " + boxedScore); System.out.println("Unboxed score (int primitive): " + rawScore); System.out.println("Total of first two scores: " + totalScore); // --- WHAT THE COMPILER ACTUALLY GENERATES --- // You can verify this by compiling and running: javap -c AutoboxingBasics.class // You'll see invokevirtual calls to Integer.valueOf and Integer.intValue System.out.println("\nAll scores in list: " + playerScores); } }
Integer.valueOf() and intValue() calls the compiler inserted. This makes the hidden mechanism completely visible and kills any doubt about what's happening.Integer.valueOf() uses the integer cache for values -128 to 127. For values outside this range, it creates a new object every call.-XX:AutoBoxCacheMax=<size> but doing so is rare.Integer.valueOf() calls; unboxing is the compiler inserting .intValue() calls — it's not runtime magic, it's compile-time code generation you can verify with javap.int sum = 0; not Integer sum = 0;public Integer findScore(String name) returning null if not found.The Integer Cache — Why == Comparisons Will Lie to Your Face
Here's the thing that trips up even senior developers: Java caches Integer objects for values between -128 and 127. This means Integer.valueOf(100) returns the exact same object every time — not a new one. It's a JVM optimization built into the spec because small integers are used so frequently that creating millions of identical tiny objects would be wasteful.
The consequence is bizarre. Comparing two autoboxed Integer values with == works correctly for small numbers but silently fails for larger ones, because == on objects compares memory addresses, not values. Two Integer objects holding 200 are different objects at different addresses, so == returns false even though they hold the same number.
This is one of the most famous Java interview questions for good reason — it looks like a bug in the language but it's actually documented, intentional behavior. The fix is simple: always use .equals() to compare Integer objects. Never use == unless you specifically want to check object identity. The same cache behavior applies to Short, Byte, Character (0–127), and Boolean (both cached).
package io.thecodeforge.java; public class IntegerCacheDemo { public static void main(String[] args) { // --- VALUES WITHIN THE CACHE RANGE: -128 to 127 --- // Integer.valueOf(100) returns the SAME cached object both times. // So == compares the same memory address and returns true. Integer firstSmallNumber = 100; // autoboxed via Integer.valueOf(100) Integer secondSmallNumber = 100; // returns the SAME cached object System.out.println("=== Within cache range (100) ==="); System.out.println("firstSmallNumber == secondSmallNumber : " + (firstSmallNumber == secondSmallNumber)); // true — SAME object System.out.println("firstSmallNumber.equals(secondSmallNumber): " + firstSmallNumber.equals(secondSmallNumber)); // true — same value // --- VALUES OUTSIDE THE CACHE RANGE --- // Integer.valueOf(200) creates a NEW object every time. // So == compares DIFFERENT memory addresses and returns false. Integer firstLargeNumber = 200; // new Integer object created Integer secondLargeNumber = 200; // ANOTHER new Integer object created System.out.println("\n=== Outside cache range (200) ==="); System.out.println("firstLargeNumber == secondLargeNumber : " + (firstLargeNumber == secondLargeNumber)); // false — DIFFERENT objects! System.out.println("firstLargeNumber.equals(secondLargeNumber): " + firstLargeNumber.equals(secondLargeNumber)); // true — same value // --- THE SAFE APPROACH: always use .equals() for wrapper comparisons --- Integer playerLevel = 250; Integer targetLevel = 250; // WRONG way — will fail silently for values outside the cache if (playerLevel == targetLevel) { System.out.println("\n[WRONG CHECK] Levels match (== used — unreliable!)"); } else { System.out.println("\n[WRONG CHECK] Levels do NOT match (== used — lied to us!)"); } // RIGHT way — always use .equals() to compare wrapper object values if (playerLevel.equals(targetLevel)) { System.out.println("[CORRECT CHECK] Levels match (.equals() used — reliable)"); } } }
-XX:AutoBoxCacheMax. Some frameworks (e.g., some application servers) increase this value, making the bug even more subtle.Dm: Use of == to compare Integer objects should be treated as an error in CI, not a warning.Performance Pitfalls — When Autoboxing Quietly Kills Your Loop
Autoboxing feels invisible, but it isn't free. Each conversion creates a heap object (except cached values), which means more garbage for the GC to collect. In a tight loop that runs thousands or millions of times, unnecessary autoboxing can turn a fast operation into a slow one without a single obvious line of code to blame.
The classic trap is accidentally using a wrapper type as an accumulator in a loop. If you declare Long totalRevenue instead of long totalRevenue, every single addition operation unboxes the Long, adds the primitive, then autoboxes the result back into a new Long object. A loop running a million times creates a million short-lived objects the GC must track and collect.
The rule of thumb is simple: use primitives for local variables and computation. Use wrapper types only when the API requires it — for collections, generics, method signatures that return null as a 'no value' signal, or database/JSON mapping where null is meaningful. This distinction is what modern Java engineers mean when they talk about being intentional with types.
package io.thecodeforge.java; public class AutoboxingPerformance { private static final int TRANSACTION_COUNT = 1_000_000; public static void main(String[] args) { // --- SLOW VERSION: wrapper type used as accumulator --- // Every += operation does THREE things: // 1. Unbox: totalRevenueSlow.longValue() // 2. Add: result = longValue + nextAmount // 3. Rebox: totalRevenueSlow = Long.valueOf(result) // This creates 1,000,000 temporary Long objects on the heap. Long totalRevenueSlow = 0L; long startSlow = System.nanoTime(); for (int transactionIndex = 0; transactionIndex < TRANSACTION_COUNT; transactionIndex++) { totalRevenueSlow += 1L; // hidden autobox/unbox on EVERY iteration } long durationSlowMs = (System.nanoTime() - startSlow) / 1_000_000; // --- FAST VERSION: primitive used as accumulator --- // No objects created. Pure stack arithmetic. GC never involved. long totalRevenueFast = 0L; long startFast = System.nanoTime(); for (int transactionIndex = 0; transactionIndex < TRANSACTION_COUNT; transactionIndex++) { totalRevenueFast += 1L; // simple primitive addition — no boxing } long durationFastMs = (System.nanoTime() - startFast) / 1_000_000; // --- RESULTS --- System.out.println("=== Performance Comparison ==="); System.out.println("Transactions processed: " + TRANSACTION_COUNT); System.out.println("Slow (Long accumulator): " + durationSlowMs + " ms"); System.out.println("Fast (long accumulator): " + durationFastMs + " ms"); System.out.println("Both totals match: " + totalRevenueSlow.equals(totalRevenueFast)); System.out.println(); System.out.println("Tip: The difference grows as TRANSACTION_COUNT grows."); System.out.println("In a financial system processing millions of records,"); System.out.println("this single type choice has real consequences."); } }
Long totalRevenueSlow = 0L creates 1,000,000 Long objects for 1M iterations. The fast version with long totalRevenueFast creates zero objects.The NullPointerException Nobody Expects — Null Unboxing in Practice
Here's the sneakiest autoboxing bug: unboxing a null wrapper causes a NullPointerException that looks like it came from nowhere. When Java tries to call .intValue() on a null Integer reference, it throws. The stack trace points at a line that looks like plain arithmetic or a simple variable assignment — no obvious null check, no object method call in your code. That's exactly why it's so confusing.
This pattern appears constantly in real code. A method returns Integer (with null meaning 'no data found'). The caller assigns it to an int variable. Boom. Or a Map lookup returns null for a missing key, and the result is immediately used in a calculation.
The fix isn't to avoid nullable wrappers — they're genuinely useful for signalling absence of a value. The fix is to always validate before unboxing when the wrapper could be null. Use null checks, Optional<Integer>, or provide a default with Objects.requireNonNullElse(). Understanding that unboxing is a hidden method call is the mental model that makes these bugs obvious before they bite you.
package io.thecodeforge.java; import java.util.HashMap; import java.util.Map; import java.util.Objects; public class NullUnboxingDemo { // Simulates a database lookup — returns null when the player doesn't exist static Integer getPlayerHighScore(String playerName) { Map<String, Integer> scoreDatabase = new HashMap<>(); scoreDatabase.put("alice", 4500); scoreDatabase.put("bob", 3200); // Note: "charlie" is not in the database — lookup returns null return scoreDatabase.get(playerName); } public static void main(String[] args) { // --- THE HIDDEN NPE TRAP --- // getPlayerHighScore returns Integer (nullable). // Assigning to int triggers unboxing: null.intValue() — NPE! try { int charlieScore = getPlayerHighScore("charlie"); // null gets unboxed here System.out.println("Charlie's score: " + charlieScore); } catch (NullPointerException npe) { System.out.println("[CAUGHT] NPE from unboxing null Integer!"); System.out.println("The line looked safe but null.intValue() was called."); } // --- FIX 1: null check before unboxing --- Integer charlieRawScore = getPlayerHighScore("charlie"); if (charlieRawScore != null) { int safeScore = charlieRawScore; // safe to unbox — we know it's not null System.out.println("\n[FIX 1] Charlie's score: " + safeScore); } else { System.out.println("\n[FIX 1] Charlie has no recorded score yet."); } // --- FIX 2: provide a default value using Objects.requireNonNullElse --- Integer aliceRawScore = getPlayerHighScore("alice"); int aliceScore = Objects.requireNonNullElse(aliceRawScore, 0); // 0 if null System.out.println("[FIX 2] Alice's score (default 0 if absent): " + aliceScore); int unknownPlayerScore = Objects.requireNonNullElse( getPlayerHighScore("dave"), 0); System.out.println("[FIX 2] Dave's score (default 0 if absent): " + unknownPlayerScore); } }
int total = calculatePrice() + calculateTax() where both methods return Integer).Objects.requireNonNullElse(wrapper, defaultValue) before unboxing, or use Optional to make nullability explicit.Java Primitive Types and Their Wrapper Classes — The Cheat Sheet You'll Memorise
Every autoboxing and unboxing disaster traces back to one thing: not knowing which primitive maps to which wrapper. The compiler hides this, but the JVM doesn't forgive ignorance.
There are exactly eight primitive types in Java, each with a corresponding wrapper class in java.lang. Boolean wraps boolean, Byte wraps byte, Short wraps short, Character wraps char, Integer wraps int, Long wraps long, Float wraps float, Double wraps double. That's it. No more. No less.
Notice the pattern: primitive names are lowercase, wrappers start with a capital. Except for char → Character and int → Integer. Those two are the ones that burn juniors who try to guess the naming convention.
When the compiler inserts autoboxing or unboxing, it's calling valueOf() and xxxValue() behind the scenes. Knowing these pairs by heart means you catch implicit conversions before they hit production. Every performance issue with autoboxing starts with someone treating an int like an Integer in a tight loop. Map the types, avoid the trap.
// io.thecodeforge — java tutorial public class WrapperMapping { public static void main(String[] args) { // Autoboxing: primitive -> wrapper via valueOf() Integer wrapped = 42; // int → Integer Boolean boolWrapped = true; // boolean → Boolean Character charWrapped = 'X'; // char → Character (not Char) // Unboxing: wrapper -> primitive via xxxValue() int unwrapped = wrapped; // Integer → int boolean boolUnwrapped = boolWrapped; // Boolean → boolean // The two that break naming conventions System.out.println(int.class.getName()); // int System.out.println(Integer.class.getName()); // java.lang.Integer System.out.println(char.class.getName()); // char System.out.println(Character.class.getName()); // java.lang.Character } }
The Generic Generational Trap — Autoboxing in Collections You Never Wrote
Here's where autoboxing slaps you when you least expect it: generics. The Java compiler forces collections to use wrapper types. You write List<Integer>, you push ints into it, and the compiler silently calls Integer.valueOf() for every single element.
This isn't just about your code. It's about the code you never wrote. Third-party libraries that return List<Integer> when you wanted int[]? You're paying autoboxing tax on every access. Sorting? Iterating? Each get() call triggers an implicit unboxing. Each put() triggers an autobox.
The real grief shows up in maps. Try HashMap<Integer, Double> in a data processing pipeline that runs over a million entries. You're allocating an Integer and a Double object—two heap allocations—every time you insert. The GC will remind you why you should have used a primitive collection library or a plain array.
Production lesson: if your collection size exceeds 10,000 elements and you see GC pressure, profile it. Chances are autoboxing is drowning your heap in short-lived wrapper objects. Trove, Eclipse Collections, or plain arrays exist for a reason. Use them.
// io.thecodeforge — java tutorial import java.util.*; public class AutoboxingInCollections { public static void main(String[] args) { // Hidden autoboxing: int to Integer, double to Double Map<String, Double> priceMap = new HashMap<>(); priceMap.put("server", 1499.99); // boxed twice: String and Double // Unboxing on every get Double rawPrice = priceMap.get("server"); // gets Double object double actualPrice = rawPrice; // unboxing calls doubleValue() System.out.println("Price: " + actualPrice); // The expensive part: bulk operations with autoboxing List<Integer> ids = new ArrayList<>(); for (int i = 0; i < 10_000; i++) { ids.add(i); // 10,000 Integer objects created on heap } long sum = 0; for (int id : ids) { // each iteration unboxes intValue() sum += id; } System.out.println("Sum: " + sum); } }
List<Integer> + for-each loop + 100,000+ elements = GC nightmare. Each iteration allocates an Integer on add and unboxes it on get. Switch to int[] or a primitive-backed collection library for tight loops.The Intermittent Integer Comparison That Corrupted Financial Reports
== and returning false. The same code compared small values (e.g., 50) correctly. The team saw the failure only on accounts with >127 transactions. The bug was intermittent by value, not by timing.== worked because they'd tested with small numbers and it passed. They didn't know about the integer cache. They also assumed that since both Integers came from the same source (autoboxing of ints from a database query), they would be the same object. They didn't know that Integer.valueOf(200) creates a new object each time.if (storedCount == currentCount) { ... } where both were Integer objects from different map lookups. For values between -128 and 127, JVM reuses cached objects, so == works. For values 128 and above, JVM creates new objects each time. The comparison failed because the two Integer objects were different heap objects with the same value. The bug was completely invisible until a day with 200+ transactions occurred. The reconciliation logic used == for equality, leading to incorrect matches and corrupt reports..equals(): if (storedCount.equals(currentCount)).
2. Added a project-wide lint rule: -Xlint:unchecked and SpotBugs rule Dm: Use of == to compare Integer objects.
3. For high-performance paths, unboxed to primitive int before comparison: int stored = storedCount; int current = currentCount; if (stored == current) (null-checked first).
4. Updated the team's coding standards: 'Never use == to compare wrapper objects. Always use .equals().'- The integer cache causes
==to work for -128..127 and fail for other values. This is the most common autoboxing bug in production Java code. - Never use
==to compare Integer, Long, Short, Byte, Character, or Boolean objects. Always use.equals(). - SpotBugs and IntelliJ inspection warnings about 'Boxed value comparison' are not noise — they catch real bugs.
- If performance is critical, unbox to primitive after null check:
int a = integerA; int b = integerB; if (a == b)
== with .equals().int total = price + tax)price or tax is an Integer that is null. Unboxing called intValue() on null. Add null checks before unboxing: if (price != null && tax != null)Long total = 0L inside loop). Change to primitives: long total = 0L. Profile with JFR to see allocation spikes.int triggers unboxing. Use Integer result = map.get(key); if (result != null) { int val = result; } or int val = map.getOrDefault(key, 0);IntArrayList from Eclipse Collections, or stick with int[]. Profile with async-profiler to confirm allocation rate.javap -c YourClass.class | grep -A10 'if_icmpne\|if_acmpne'echo 'System.out.println(IntegerCache.high);' | jshell -if (a == b) with if (a.equals(b)). For primitive comparison, unbox first: if (a != null && b != null && a.intValue() == b.intValue())javap -c YourClass.class | grep -B5 'invokevirtual.*intValue'grep -n 'Integer\|Long\|Double' src/ | grep -v '<' | grep -v '>'if (price != null && tax != null) { int total = price + tax; }. For Map.get, use map.getOrDefault(key, 0) or Objects.requireNonNullElse(map.get(key), 0)grep -n 'for.*Long\|for.*Integer' src/jcmd <pid> GC.heap_info | grep -A5 'Integer\|Long'Long total = 0L to long total = 0L. Change Integer sum = 0 to int sum = 0. Use primitives for loop variables and accumulators.grep -n 'public.*Integer' src/ | grep -v 'Integer.*,.*\['grep -n 'int\s\+\w\+\s*=\s*\w+\.get' src/Integer result = service.getValue(); if (result != null) { int val = result; }. Or change API to return Optional<Integer> or primitive with sentinel (-1).jmap -histo <pid> | grep -E '\[I\|java.lang.Integer'echo 'Integer object: 16 bytes header + 4 bytes int = 20 bytes; int[] = 4 bytes per element'List<Integer> with int[] for large collections. Use IntArrayList from Eclipse Collections, IntList from FastUtil, or IntBuffer from Java NIO.| Aspect | Primitive (int, long, etc.) | Wrapper Class (Integer, Long, etc.) |
|---|---|---|
| Memory location | Stack (fast access, no GC) | Heap (requires GC, object overhead) |
| Memory per value (int/Integer) | 4 bytes | 16 bytes (object header) + 4 bytes int = 20 bytes typical (~16 with compressed oops) |
| Can be null | No — always has a value | Yes — null means 'no value' |
| Use in Collections/Generics | Not allowed directly (except via arrays) | Required (List<Integer>, Map<String, Integer>) |
| Comparison operator == | Compares value — always correct | Compares object identity — unreliable, use .equals() |
| Performance in loops | Fast — no allocations | Slower — creates heap objects each conversion (except cache) |
| Default value for fields | 0, false, 0.0 (type dependent) | null (can cause NPE on unbox) |
| Methods available | None — it's just a value | parseInt(), valueOf(), compareTo(), toString(), etc. |
| Autoboxing overhead | None | Integer.valueOf() call (cached for -128..127, new object otherwise) |
| When to choose it | Local vars, counters, math-heavy code, performance-critical paths | Collections, nullable fields, API return types, ORM/JPA entities |
| File | Command / Code | Purpose |
|---|---|---|
| io | public class AutoboxingBasics { | What Autoboxing and Unboxing Actually Are (Under the Hood) |
| io | public class IntegerCacheDemo { | The Integer Cache |
| io | public class AutoboxingPerformance { | Performance Pitfalls |
| io | public class NullUnboxingDemo { | The NullPointerException Nobody Expects |
| WrapperMapping.java | public class WrapperMapping { | Java Primitive Types and Their Wrapper Classes |
| AutoboxingInCollections.java | public class AutoboxingInCollections { | The Generic Generational Trap |
Key takeaways
Integer.valueOf() calls; unboxing is the compiler inserting .intValue() callsCommon mistakes to avoid
5 patternsUsing == to compare Integer objects
if (a != null && b != null && a.intValue() == b.intValue()). Add SpotBugs rule to block == on boxed types in CI.Declaring a wrapper type as a loop accumulator (e.g., `Long total = 0L`)
Integer.valueOf() as a hotspot.long total = 0L. The wrapper is not needed. Only use wrapper if null is meaningful or the collection requires it.Unboxing a wrapper returned from a Map or database call without a null check
Integer result = map.get(key); if (result != null) { int val = result; }. For Map, use map.getOrDefault(key, 0) if default value acceptable. Use Objects.requireNonNullElse(wrapper, defaultValue) for nullable wrappers.Using Integer where primitive would suffice in a hot path
int fields in performance-critical classes, not Integer.Assuming Integer cache size is always -128..127
Interview Questions on This Topic
What is the integer cache in Java, and how does it affect == comparisons between autoboxed Integer values? Can you show an example where == gives different results for Integer variables holding 100 vs 200?
Integer.valueOf() method. When you autobox an int within this range, the same cached object is returned. For values outside this range, a new Integer object is created each time. Example: Integer a = 100; Integer b = 100; a == b returns true because both reference the same cached object. Integer c = 200; Integer d = 200; c == d returns false because they are two different objects on the heap. Using == compares object references (memory addresses), not the numeric values. This is why you should always use .equals() to compare the values of wrapper objects. The cache range can be extended with the JVM flag -XX:AutoBoxCacheMax, but relying on this is fragile.What happens at the bytecode level when you write 'int x = someIntegerObject'? What method does the compiler insert, and what happens if someIntegerObject is null?
someIntegerObject.intValue(). This is unboxing. The generated bytecode will have an invokevirtual instruction calling java/lang/Integer.intValue(). If someIntegerObject is null, the JVM throws a NullPointerException at the point of the .intValue() call. However, the source code line appears to be a simple assignment, making the NPE confusing. This is why developers see NPE on lines that look like int x = obj; with no obvious method call. In Java 14+, the NPE message includes the exact expression that was null, e.g., 'Cannot unbox a null value'.You have a loop that runs 10 million times and accumulates a sum. Someone wrote 'Long total = 0L' as the accumulator. What is the performance problem, why does it happen, and what is the one-character fix?
Long is a wrapper object, while long is a primitive. Inside the loop, total += amount unboxes Long to long via total.longValue(), adds the amount, then autoboxes the result back to a new Long object via Long.valueOf(...) on each iteration. This creates 10 million temporary Long objects on the heap, causing high memory allocation, GC pressure, and CPU overhead for boxing/unboxing. The fix: change Long total = 0L to long total = 0L. That's changing the 'L' to lowercase 'l' (one character change, but semantically primitive vs wrapper). This eliminates all heap allocations inside the loop, making the code pure stack-based arithmetic and significantly faster (typically 5-10x improvement).Why does Automatic (un)boxing exist in Java? Explain the trade-offs between primitives and wrapper classes.
Integer.valueOf(i) and integer.intValue(), leading to verbose, error-prone code. Autoboxing makes the conversion automatic, improving readability. Trade-offs: primitives are faster (no heap allocation), use less memory (int = 4 bytes vs Integer ≈ 16-20 bytes due to object header), and cannot be null (no NPE risk). Wrappers are needed for collections (List<Integer>), generics, and any situation where null is meaningful (e.g., database column that can be NULL). The cost of boxing is heap allocation and GC pressure, especially in loops. Project Valhalla aims to add primitive generics to Java, which would make this distinction unnecessary in the future, but currently, you must choose explicitly.Frequently Asked Questions
Java's Collections framework and generics were designed around objects, and primitives aren't objects in the JVM's type system. Rather than redesign the entire language, Java 5 introduced autoboxing as a compiler-level bridge so you can write natural-looking code without manually calling Integer.valueOf() everywhere. Project Valhalla (future Java) aims to bring primitives into generics properly (primitive classes), which will eventually make much of this moot.
Autoboxing is fine for occasional conversions — adding items to a list, returning a value from a method. It only becomes a real problem in tight loops or high-frequency code where thousands of wrapper objects get created and discarded per second. Profile first, optimize second. Using ArrayList<Integer> for a list you iterate once is perfectly fine. Using Long as a loop counter in a billing calculation that runs millions of times is not.
Integer.valueOf() is the factory method that uses the integer cache — it returns a cached object for values between -128 and 127. new Integer() (deprecated since Java 9, removed in Java 17) always creates a brand new heap object. Autoboxing always uses Integer.valueOf(), which is why the cache behaviour applies. Never use new Integer() in modern Java code. For string parsing, use Integer.parseInt() for primitive, Integer.valueOf() for Integer object.
Several strategies: (1) Use map.getOrDefault(key, defaultValue) which returns primitive-compatible default: int val = map.getOrDefault("key", 0); (but this requires Map<String, Integer> and default must be Integer). (2) Use Integer wrapped = map.get(key); if (wrapped != null) { int val = wrapped; }. (3) Use int val = Objects.requireNonNullElse(map.get(key), 0);. (4) Use Optional.ofNullable(map.get(key)).orElse(0). The choice depends on whether 0 is a valid value that could be confused with 'not present'.
20+ years shipping production Java in banking & fintech. Notes here come from systems that actually shipped.
That's Java Basics. Mark it forged?
5 min read · try the examples if you haven't