Skip to main content
Java Output Prediction Hard +35 XP

Integer Caching

What does this comparison return?

JavaRead-only
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b);
System.out.println(c == d);

What is the output?

Show answer and explanation

Answer: true then false

Java caches Integer objects for values -128 to 127. Beyond that range, new objects are created, so == compares references.

Hint: Java caches small integers.

What this challenge tests

Output Prediction in Java: wrapper-classes, caching, gotchas.

Related challenges