Quick revision
The short answer
It prints ab and then eq. For Integer objects, == compares references, not numbers. Autoboxing calls Integer.valueOf, which returns cached objects for values from -128 to 127, so a and b are the same object and a == b is true, while 128 is outside the cache, so c and d are two different objects and c == d is false. new String("x") always creates a new object, so s == "x" is false, and s.equals("x") compares the characters and is true. For objects we should compare with equals or Objects.equals, and compare boxed numbers by their int values.
What to remember
In this answer, we saw that == compares references for objects. Autoboxing uses Integer.valueOf, which returns cached objects from -128 to 127, so a == b is true while c == d is false. new String("x") creates a new object, so it is not == to the pooled literal, but equals compares the characters and prints eq. We compare objects with equals or Objects.equals, compare boxed numbers by their int values, and let static analysis flag reference comparisons.
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.
It prints ab and then eq. For Integer objects, == compares references, not numbers. Autoboxing calls Integer.valueOf, which returns cached objects for values from -128 to 127, so a and b are the same object and a == b is true, while 128 is outside the cache, so c and d are two different objects and c == d is false. new String("x") always creates a new object, so s == "x" is false, and s.equals("x") compares the characters and is true. For objects we should compare with equals or Objects.equals, and compare boxed numbers by their int values.
Why this question is asked
This snippet catches experienced developers because it behaves differently for different numbers: the same code that works in a unit test with small ids fails in production when ids pass 127. Interviewers ask it because the explanation needs three ideas at once: reference versus value comparison, the Integer cache behind autoboxing, and how string literals and new String differ. In this answer, we will recap what == means for primitives and objects, walk through each comparison in the snippet, look at the Integer cache in detail, and finish with how to compare correctly and how tools catch these bugs.
1A Quick Recap: What == Compares
For primitive types like int, == compares values: 128 == 128 is true. For any object type, including Integer and String, == compares references, the addresses of the objects. It is true only when both sides point at the very same object. Two different objects with the same content are not == to each other.
To compare contents, objects have the equals method. String.equals compares the characters, and Integer.equals compares the int values. So the question for each line of the snippet is: are these two variables pointing at the same object, or just at equal ones?
2Why a == b Is True but c == d Is False
Integer a = 127 looks like a plain assignment, but 127 is an int and a is an Integer, so the compiler inserts autoboxing: it writes Integer a = Integer.valueOf(127). The Java Language Specification requires that boxing an int between -128 and 127 always gives the same object, and Integer.valueOf meets that rule with a cache of 256 pre-built Integer objects. So a and b both get the cached object for 127, and a == b is true.
128 is outside the cache, so each call to Integer.valueOf(128) creates a new object. c and d hold equal values in two different objects, and c == d is false. The comparison worked for 127 only by accident of the cache, not because == compares numbers.
| Variable | Created by | Object | Result of == |
|---|---|---|---|
| a | Integer.valueOf(127) | cached Integer 127 | a == b: true, same object |
| b | Integer.valueOf(127) | cached Integer 127 | |
| c | Integer.valueOf(128) | new Integer 128, object 1 | c == d: false, different objects |
| d | Integer.valueOf(128) | new Integer 128, object 2 | |
| s | new String("x") | new String object | s == "x": false |
| "x" | string literal | pooled String | s.equals("x"): true |
Notice the fifth line. When one side of == is a primitive, the Integer is unboxed and the values are compared, so c == 128 is true. The trap only exists when both sides are Integer objects. The upper limit of the cache can be raised with the JVM option -XX:AutoBoxCacheMax, which is one more reason never to rely on it: the same code can behave differently on two servers.
3Why s == "x" Is False
String literals in Java are interned: every literal "x" in the program refers to one shared String object in the string pool. But new String("x") always creates a new object, a copy of the pooled one, by definition of new. So s points at the copy, the literal points at the pooled original, and s == "x" is false.
The else if then runs s.equals("x"), which compares the characters one by one. They are the same, so it prints eq. The full output of the snippet is therefore two lines, ab and eq.
Notice the third line: intern() returns the pooled instance, so it is == to the literal. That is how the pool works, but it is not a way to fix comparisons; equals is.
4How to Compare Correctly
For objects, always compare contents with equals. When either side may be null, use Objects.equals(x, y), which returns true when both are null and false when only one is, without throwing. For boxed numbers, either compare with equals, or unbox explicitly with intValue(), or better still, declare the variables as int when they cannot be null, so == compares values naturally.
The first line is the version that reaches production: ids are boxed because they come from a map, an entity or a generic collection, the tests use ids below 128, and the check silently fails for real customers. The same caching applies to Long, Short, Byte and Character values in the same small range.
5How Tools Catch It
Because this bug depends on the data, tests often miss it, so static analysis is the reliable safety net. SpotBugs reports suspicious reference comparisons of boxed values and strings compared with == (for example its RC_REF_COMPARISON and ES_COMPARING_STRINGS_WITH_EQ patterns), and IntelliJ IDEA has inspections for numbers and strings compared with == instead of equals. Turning these on in the shared build makes the mistake fail early instead of in production.
6Follow-up questions
Why does the cache cover exactly -128 to 127?
The Java Language Specification requires boxing to return identical objects for int values in that range, because small numbers are boxed constantly and sharing them saves memory and allocation. The range covers the values of a byte. The JVM may cache more, which is what -XX:AutoBoxCacheMax allows, but code must never rely on either range.
Does new Integer(127) == new Integer(127)?
No, new always creates a distinct object, so it is false. The Integer constructors were deprecated in Java 9 and marked for removal in Java 16, precisely because they bypass the cache; Integer.valueOf should be used instead.
Is it safe to compare enums with ==?
Yes. Each enum constant is a single instance, so == is correct, cannot throw on null, and is checked by the compiler for type compatibility. That is the one common case where == on objects is the recommended form.
7Conclusion
In this answer, we saw that == compares references for objects. Autoboxing uses Integer.valueOf, which returns cached objects from -128 to 127, so a == b is true while c == d is false. new String("x") creates a new object, so it is not == to the pooled literal, but equals compares the characters and prints eq. We compare objects with equals or Objects.equals, compare boxed numbers by their int values, and let static analysis flag reference comparisons.
Open with
It prints ab and then eq. The reason is that == on objects compares references, not values. Integer a = 127 is autoboxed with Integer.valueOf, and Integer.valueOf returns cached objects for values from -128 to 127, which the language requires.
Then, if they want more
So a and b are the same cached object and a == b is true. 128 is outside that cache, so c and d are two separate Integer objects holding the same value, and c == d is false. That is why this kind of bug passes tests with small ids and fails in production.
For the strings, new String always creates a new object, while the literal x comes from the string pool, so s == x is false, and then s.equals compares the characters and prints eq.
To fix comparisons like these, I use equals, or Objects.equals when either side can be null, and for boxed numbers I compare intValue or declare the variables as int if they can never be null. If one side of == is a primitive, Java unboxes and compares values, so the trap is specifically two boxed objects.
I would also enable static analysis, SpotBugs or IntelliJ inspections, which flag reference comparisons of boxed types and strings.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16, Java 9
- 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/operators-and-control-flow/81d816f7a7/