Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

What does this print, and why?

Java
Integer a = 127, b = 127, c = 128, d = 128;
System.out.println(a == b);
System.out.println(c == d);
System.out.println('a' + 'b');
System.out.println("" + 'a' + 'b');
short s = 1;
s += 1; // and what about
s = s + 1;

It prints true, false, 195 and ab. a == b is true because autoboxing calls Integer.valueOf, which returns cached objects for -128 to 127, while 128 is outside the default cache, so c and d are two different objects. 'a' + 'b' is int arithmetic (97 + 98), and "" + 'a' + 'b' is string concatenation. Finally, s += 1 compiles because compound assignment includes a hidden cast to short, while s = s + 1 does not compile, because s + 1 is an int.

Background to this question

These five lines pack four of Java's quieter rules into one puzzle: how boxing creates objects, what == compares, how char and short values are promoted to int in arithmetic, and how compound assignment differs from plain assignment. Each rule causes real bugs: comparisons of boxed ids that pass in tests with small numbers and fail in production with large ones, or log lines that print a number where we expected text. In this answer, we will recap boxing and the Integer cache, go through each line with its output, and explain the rule behind each result.

1A Quick Recap of Boxing and ==

Java has primitive types, such as int and char, which hold plain values, and wrapper classes, such as Integer and Character, which are objects holding one value. Autoboxing is the automatic conversion from a primitive to its wrapper. When we write Integer a = 127, the compiler turns it into Integer a = Integer.valueOf(127).

The == operator means different things for the two kinds of types. For primitives it compares values. For objects it compares references: it is true only when both sides are the same object in memory. To compare the values inside two objects we use equals().

Integer.valueOf does not always create a new object. The Javadoc requires it to cache the values from -128 to 127, so every call with a number in that range returns the same shared Integer object. For values outside the range, it creates a new object each time, unless the cache has been made larger.

Which boxed values are cached by valueOf
WrapperCached rangeConfigurable?
Integer-128 to 127upper bound via -XX:AutoBoxCacheMax
Long, Short, Byte-128 to 127no
Character0 to 127no
BooleanTRUE and FALSEtwo constants only
Float, Doublenoneevery valueOf creates a new object
The ranges are the ones the Javadoc requires; a JVM may cache more, so never rely on ==.

2Lines 1 and 2: 127 Versus 128

Here are the first two lines with their output.

Java
Integer a = 127, b = 127, c = 128, d = 128; // each is Integer.valueOf(...)
System.out.println(a == b); // true: both are the cached Integer for 127
System.out.println(c == d); // false: 128 is outside the cache, two objects
System.out.println(c.equals(d)); // true: equals compares the values

a and b both come from Integer.valueOf(127), which returns the same cached object, so == finds the same reference and prints true. c and d come from Integer.valueOf(128), and 128 is outside the default cache, so each call creates a new object. Two different objects mean == prints false, even though they hold the same number.

Notice the word default. The upper bound of the Integer cache can be raised with the JVM option -XX:AutoBoxCacheMax=1000, and then c == d prints true. The result of == on boxed values therefore depends on the value and on how the JVM was started. That is why the only safe rule is never to compare boxed values with ==; use equals(), or unbox to primitives first. This bug is common with Long ids from a database: tests with ids below 128 pass, and production with id 4,512 fails.

3Lines 3 and 4: char Arithmetic Versus String Concatenation

The next two lines look almost the same but print very different things.

Java
System.out.println('a' + 'b'); // 195
System.out.println("" + 'a' + 'b'); // ab

A char in Java is a 16-bit number, the UTF-16 code of the character. 'a' is 97 and 'b' is 98. The + operator on two char values is numeric addition, and Java promotes both operands to int before adding, a rule called binary numeric promotion. So 'a' + 'b' is 97 + 98, which is the int 195, and println(int) prints 195.

In the second line, + is evaluated from left to right. The first operation is "" + 'a'. One operand is a String, so this + is string concatenation and produces "a". Then "a" + 'b' is again concatenation, producing "ab". The empty string at the start changes the meaning of every + that follows. If we wrote 'a' + 'b' + "", we would get "195", because the first + is still numeric.

4Line 5: Why s += 1 Compiles and s = s + 1 Does Not

The last part is about assignment.

Java
short s = 1;
s += 1; // compiles; s is now 2
s = s + 1; // compile error: incompatible types: possible lossy conversion from int to short
s = (short) (s + 1); // compiles; this is what s += 1 really means

The expression s + 1 follows the same promotion rule as the char addition: short operands are promoted to int, so the result is an int. Assigning an int to a short could lose information, so the compiler refuses s = s + 1 without an explicit cast.

A compound assignment such as s += 1 is defined differently. The language specification says E1 op= E2 means E1 = (T) ((E1) op (E2)), where T is the type of E1. So the cast to short is built in, and the statement compiles. The catch is that the hidden cast also hides overflow: if s were 32767, s += 1 would silently make it -32768, while the plain assignment would at least have forced us to write the cast and think about it.

5Follow-up questions

What does Long.valueOf cache?

Long.valueOf caches the values from -128 to 127, the same range as Integer. Unlike the Integer cache, its size cannot be changed with a JVM option. So two Long ids of 100 boxed separately are the same object and compare equal with ==, while two ids of 4,512 are different objects, and == returns false.

Is new Integer(127) == new Integer(127) also true?

No. The constructor always creates a new object, so == is false. The Integer constructors were deprecated in Java 9 and marked for removal in Java 16, in favour of valueOf, partly because they bypass the cache.

Does == between an Integer and an int compare values?

Yes. When one operand is a primitive int, the Integer is unboxed and the values are compared. But if the Integer is null, unboxing throws a NullPointerException, which is another reason to be careful with mixed comparisons.

6Conclusion

In this answer, we saw that autoboxing calls Integer.valueOf, which returns cached objects for -128 to 127, so a == b is true and c == d is false by default, though the cache can be enlarged with -XX:AutoBoxCacheMax; boxed values must be compared with equals. Two chars added together are promoted to int, giving 195, while a leading empty string turns every + into concatenation, giving ab. Compound assignment includes a hidden cast, so s += 1 compiles and s = s + 1 does not.

What to say in the interviewAbout 80 seconds aloud

Open with

The output is true, false, 195 and ab. For the first two, autoboxing turns Integer a = 127 into Integer.valueOf(127), and valueOf returns cached objects for minus 128 to 127.

Then, if they want more

So a and b are the same object and == is true, but 128 is outside the default cache, so c and d are two separate objects and == compares references, giving false. The cache upper bound can be raised with -XX:AutoBoxCacheMax, so the result even depends on JVM flags, which is why I always compare boxed values with equals.

This shows up in real bugs with Long ids that pass tests with small numbers. For 'a' + 'b', chars are numbers, 97 and 98, and binary numeric promotion makes it int addition, so it prints 195. With the empty string first, the first plus is string concatenation and everything after it is concatenation too, so it prints ab.

For the short: s plus 1 is promoted to int, so s = s + 1 fails to compile with a lossy conversion error, while s += 1 compiles because compound assignment is defined as including a cast back to the variable's type. That hidden cast also hides overflow, so 32767 plus 1 quietly becomes minus 32768.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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/coding-problems/6d6a07f003/