Java Integer Cache Pop Quiz
Why is this Languages meme funny?
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.
Description
A dark-background meme shows a mock exam: "The teacher: the test isn't that hard" followed by "The test:" and a Java code snippet. The code defines `Integer a = 0; Integer b = 0; while(a==b) { ++a; ++b; }` and then prints `System.out.println("The output is "+a);`, with a 100-point prompt asking for the output. The joke relies on Java autoboxing and `Integer` caching: small boxed integers compare reference-equal via `==`, but once both values become 128 they are usually boxed as separate objects, so the loop exits and prints 128.
Comments
98Comment deleted
The answer is 128, unless someone tuned `IntegerCache` and turned the exam into a JVM configuration audit.
There will be no output Comment deleted
Because the program never reaches output statement Comment deleted
You don’t say? Comment deleted
He's wrong the output is 0. Read the code again or run it yourself Comment deleted
comparing objects by reference ... Comment deleted
C++ style... Comment deleted
Just redefine that operator Comment deleted
Output: ⊥ Comment deleted
if this was c++ you could write whatever and it will count Comment deleted
писькодроч Comment deleted
А я чёт тоже недогнал чё за ответ будет, лол Comment deleted
output is 0 Comment deleted
y tho Comment deleted
Пачиму?? Comment deleted
Переполнение буфера может? Comment deleted
Ноль, потому что сравниваются ссылочные типы на равенство, которое даст false Comment deleted
java java Comment deleted
Exactly, bro Comment deleted
wait, is that == operator is functionaly useless? Comment deleted
Thankfully, I write in Kotlin Comment deleted
є кеш інтів, тому 128 Comment deleted
ah bruh. That's a big brain meme if I've ever seen one. Comment deleted
Indeed Comment deleted
Dang, and they complain about PHP's quirks? Fml I would not be able to figure that if I had a bug in software like that Comment deleted
python has it too! Comment deleted
Аааааа, Comment deleted
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. Comment deleted
Otput will be 128! Comment deleted
that's a big number ngl Comment deleted
lol Comment deleted
https://wiki.owasp.org/index.php/Java_gotchas#Immutable_Objects_.2F_Wrapper_Class_Caching Comment deleted
bruh Comment deleted
wait youre here Comment deleted
oh you are in comments xd Comment deleted
i know the python way Comment deleted
same Comment deleted
Hmm Comment deleted
Java, go fuck yourself. I'm going to use python or rust instead Comment deleted
it's the same in python… Comment deleted
well, if you compare by id, that is Comment deleted
s/rust/C Comment deleted
a = -5 b = -5 while (a := a + 1) is (b := b + 1): pass print(i) Comment deleted
Yeah, but in python you explicitly use reference comparison Comment deleted
Yeah, it's doesn't shoot you in the feet without warning Comment deleted
yes i agree java bad Comment deleted
hey guess what. i learnt nim instead of kotlin. and i dont regret it Comment deleted
fuck you Comment deleted
kotlin is best Comment deleted
jvm tho Comment deleted
Yep Comment deleted
jvm is literally faster than C Comment deleted
HAHAHAHAHAN'T Comment deleted
refer to the conversation above Comment deleted
what, the test code with a and b? Comment deleted
no, with Slava S. Comment deleted
what Comment deleted
if you are not in chat then the post "C++ devs when their thousand line code..." Comment deleted
oh i'm here Comment deleted
what's slava s Comment deleted
https://benchmarksgame-team.pages.debian.net/benchmarksgame/performance/mandelbrot.html Comment deleted
lol even node.js is faster Comment deleted
Lol Comment deleted
this page literally says you have to read the code to see which are threaded Comment deleted
And indeed both Java and C programs are multithreaded Comment deleted
Yes Comment deleted
PHP POWA Comment deleted
Shut the fuck up... Comment deleted
Just kidding, but C# or C++ all the way Comment deleted
Oh, still I don't like C# nor C++ but I mean still better than that shitwell Comment deleted
They both suck Comment deleted
btw this looks completely unlogical Comment deleted
most tests do. ever did a math test? yeah it's the same with programming. Comment deleted
a = 0 b = 0 while a==b: a += 1 b += 1 print ("The output is: {output}".format(output=a)) Comment deleted
that'd be infinity Comment deleted
no Comment deleted
a = -5 b = -5 while (a := a + 1) is (b := b + 1): pass print(a, b) Comment deleted
walrus i must hate Comment deleted
a bit simpler would be: a=0 b=0 while a is b: a+=1 b+=1 print(a,b) also, keep in mind that the walrus operator is new in 3.8, and therefore incompatible with most python installations. 3.6 is usually the target version. Comment deleted
fuck 3.6 it doesn't even support f strings Comment deleted
yes it does… Comment deleted
does it? i don't remember that Comment deleted
I think 3.6 is the version where they were first introduced. Comment deleted
i thought it was 3.7 Comment deleted
3.7 was the asyncio update Comment deleted
Language: ruby Source: a = b = 0 while a == b a += 1 b += 1 end puts(a) Errors: Kill signal (SIGKILL) Comment deleted
Numpy is the fastest language 😂 Comment deleted
128 :) Comment deleted
nice ! my answer was right Comment deleted
Old school rules) Comment deleted
Primer comentario en español Comment deleted
This is because under 127 all Integers have same reference Comment deleted
To will get a output when it adventually crashes Comment deleted
It will always output 0 Comment deleted
it will output 128 Comment deleted
Lol i read while a=b Comment deleted
i thought i was going insane, but you just deleted that message Comment deleted
But why 128? Comment deleted