Quick revision
The short answer
With default JVM settings it prints "true false". Autoboxing calls Integer.valueOf, which returns the same cached object for values from -128 to 127, so a and b point to one object, while 128 is outside the cache and produces two different objects. The == operator on Integer compares references, not numbers, so we should always compare wrappers with equals or by unboxing them.
What to remember
In this answer, we saw that the snippet prints "true false" because autoboxing calls Integer.valueOf, which returns cached objects for -128 to 127 and new objects outside that range, and because == between two Integer references compares identity. We also saw that the cache's upper bound can be raised with -XX:AutoBoxCacheMax, which changes the output, and that the correct comparison is equals, Objects.equals or an explicit unboxing.
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.
With default JVM settings it prints "true false". Autoboxing calls Integer.valueOf, which returns the same cached object for values from -128 to 127, so a and b point to one object, while 128 is outside the cache and produces two different objects. The == operator on Integer compares references, not numbers, so we should always compare wrappers with equals or by unboxing them.
Why this question is asked
This small puzzle hides one of the most common bugs in Java code: comparing boxed numbers with ==. It passes every test that uses small numbers and then fails in production when values grow. Interviewers ask it to see whether we know what autoboxing really does, why the cache exists, and why the answer can even change with JVM options. In this answer, we will recap autoboxing and what == means for objects, walk through each line of the snippet, look at the Integer cache and how to change it, and then see the correct way to compare wrappers.
1A Quick Recap of Autoboxing and ==
Integer is a class that wraps a primitive int in an object. Autoboxing is the automatic conversion from int to Integer that the compiler inserts when we assign a primitive to a wrapper variable, and unboxing is the conversion back. So Integer a = 127 does not store the number 127 in a; it stores a reference to an Integer object that holds 127.
The == operator has two meanings in Java. Between primitives, it compares values. Between references, it compares identity: it is true only when both variables point to the very same object. When both sides are Integer, Java does not unbox them, so == asks whether a and b are the same object.
2Walking Through the Snippet
The compiler turns each assignment into a call to Integer.valueOf. Writing it out makes the behaviour clear.
- Is n between -128 and the cache high bound?YesReturn the cached objectNoCreate a new Integer
- Cached objectshared by every caller asking for n
- New objecta distinct instance each time
3 steps in order. Play them, or step through them one at a time.
The default high bound is 127.
So a and b are equal by accident of caching, not because == compared their numbers. c and d hold the same value but are different objects, which is all that == can see.
3Why the Cache Exists and Why It Can Change
Small numbers such as 0, 1 or -1 are boxed all the time, in loops, collections and counters. Creating a new object for each would waste memory and time, so Integer keeps a small array of ready-made objects. The language specification requires that boxing any int from -128 to 127 gives the same object each time, so this part of the behaviour is guaranteed.
The upper bound, however, can be raised. Starting the JVM with -XX:AutoBoxCacheMax=1000, or setting the system property java.lang.Integer.IntegerCache.high, makes the cache cover more values. On such a JVM, the same snippet prints "true true". That alone is a strong reason never to rely on == between wrappers: the answer depends on a setting outside our code.
4Comparing Wrappers Correctly
To compare two Integer values, we use equals, or Objects.equals when either side may be null, or we unbox explicitly when we know both are present.
Notice the last line. When one side of == is a primitive, Java unboxes the other side and compares numbers. That is why mixed comparisons work and pure wrapper comparisons do not, which makes the bug even harder to spot by eye. Most IDEs and static analysers flag == between wrapper types, and it is worth turning that warning on.
5Follow-up questions
Which other wrapper types cache values, and over what ranges?
Boolean has only two instances, TRUE and FALSE, and valueOf always returns one of them. Byte caches all 256 of its values. Short and Long cache -128 to 127, and Character caches 0 to 127. Only Integer's upper bound can be raised with a JVM option. Float and Double do not cache at all, so Double.valueOf(1.0) == Double.valueOf(1.0) is false. The language only guarantees the cache for boolean, byte, char up to 127, and short and int from -128 to 127; the Long cache is an implementation detail.
Does new Integer(127) use the cache?
No. The constructor always creates a new object, so new Integer(127) == new Integer(127) is false. The wrapper constructors have been deprecated for removal since JDK 16, and we should use valueOf or autoboxing instead.
Why is c == 128 true when c == d is false?
Because in c == 128 one operand is a primitive int. Java then applies numeric promotion, unboxes c, and compares the two numbers. Only when both operands are references does == compare identity.
6Conclusion
In this answer, we saw that the snippet prints "true false" because autoboxing calls Integer.valueOf, which returns cached objects for -128 to 127 and new objects outside that range, and because == between two Integer references compares identity. We also saw that the cache's upper bound can be raised with -XX:AutoBoxCacheMax, which changes the output, and that the correct comparison is equals, Objects.equals or an explicit unboxing.
Open with
It prints true false with default settings. The reason is that Integer a = 127 is autoboxing, which the compiler turns into Integer.valueOf(127). valueOf keeps a cache of Integer objects for values from minus 128 to 127, and the language specification guarantees that range, so a and b get the same cached object and == is true. 128 is outside the default cache, so valueOf creates a new object for c and another for d.
Then, if they want more
Since both sides are Integer references, == compares identity, not value, and two different objects give false. I would also point out that the upper bound of the cache is configurable with -XX:AutoBoxCacheMax or the java.lang.Integer.IntegerCache.high property, so on a JVM started with a larger cache the same code prints true true.
That is exactly why we should never use == on wrappers: the result depends on the value and on JVM settings. The fix is equals, or Objects.equals if either side can be null, or comparing intValue() results explicitly.
Comparing an Integer with a primitive int is fine, because Java unboxes the wrapper and compares numbers, but comparing two wrappers with == is a bug waiting for a large value.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- 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 "What does this print and why?": https://scrivio-interview-library.netlify.app/java/types-and-values/87d68d830e/