Skip to content
DevMeme

Java Integer Cache Pop Quiz — Meme Explained

Java Integer Cache Pop Quiz
View this meme on DevMeme →

Level 1: The Matching Stickers

This is like two people holding matching numbered stickers. From 0 to 127, the teacher gives them the exact same shared sticker, so they match. At 128, they get two separate stickers with the same number printed on them. The numbers still match, but the stickers are not the same sticker anymore, so the game stops.

Level 2: Same Value, Different Object

Autoboxing is Java's automatic conversion between primitive values like int and wrapper objects like Integer. It is convenient because you can write Integer a = 0; and Java will turn the 0 into an Integer object for you.

The important distinction is value equality versus reference equality. a.equals(b) asks whether two Integer objects represent the same number. a == b asks whether they are literally the same object in memory. Because Java caches small boxed integers, a == b can appear to work for small values and then suddenly fail at 128.

For newer developers, this is one of those subtle bugs that teaches an uncomfortable lesson: code can be wrong even when it looks obvious. If these variables had been declared as int a = 0; int b = 0;, the loop would behave very differently. The meme is funny because the exam hides a runtime detail inside code that looks like basic counting.

Level 3: Boxed Into 128

The teacher: the test isn't that hard

100 points: write below the output of the program:

The joke is that the test looks like a tiny loop, but it is actually a trapdoor into Java's boxed primitive behavior. The code uses Integer, not int, and the loop condition is while(a==b). That single choice turns a beginner-looking exercise into a senior-developer flashback about autoboxing, reference equality, and why language gotchas should come with hazard labels.

In Java, Integer is an object wrapper around an int. The operator == behaves differently depending on what it is comparing. For primitive int values, it compares numeric value. For object references like Integer a and Integer b, it compares whether both variables point to the same object. Java also caches boxed integers in a small required range, commonly -128 through 127, so Integer.valueOf(0) through Integer.valueOf(127) can return shared objects.

That means the loop keeps running while a and b refer to the same cached object. Each ++a and ++b unboxes the current Integer, increments the primitive value, then boxes it again. For 0 through 127, both variables typically receive the same cached object for each value. When they increment to 128, the usual cache no longer applies, so a and b become two distinct Integer objects with the same numeric value. The next a==b is false, the loop exits, and the print statement gives:

The output is 128

The cruel part is that this is technically fair if the class has covered wrapper classes, but emotionally it is a mugging. The caption says the test "isn't that hard" while the actual question punishes anyone who learned == as "equals" without also learning when Java quietly swaps values for objects behind the curtain.

Comments (98)

  1. Anonymous

    The answer is 128, unless someone tuned `IntegerCache` and turned the exam into a JVM configuration audit.

  2. @sylfn

    There will be no output

  3. @sylfn

    Because the program never reaches output statement

  4. @misesOnWheels

    comparing objects by reference ...

  5. @sylfn

    C++ style...

  6. @sylfn

    Just redefine that operator

  7. @maxgraey

    Output: ⊥

  8. Deleted Account

    if this was c++ you could write whatever and it will count

  9. @KiT_BoPKiT

    писькодроч

  10. @s2504s

    А я чёт тоже недогнал чё за ответ будет, лол

  11. @mahdisml

    output is 0

  12. @s2504s

    Пачиму??

  13. @s2504s

    Переполнение буфера может?

  14. @yevhen_k

    Ноль, потому что сравниваются ссылочные типы на равенство, которое даст false

  15. @s2504s

    Аааааа,

  16. @RiedleroD

    I've seen that meme a couple of times, but I just realized: those are Integers, not ints and since Java compares Objects by reference, not by content, the loop wouldn't start and the output would be 0.

  17. @maxgraey

    Otput will be 128!

  18. @maxgraey

    https://wiki.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching

  19. @CustomIcon

    bruh

  20. Deleted Account

    Hmm

Join the discussion →

Related deep dives