Languages
Post #2516 · source on Telegram
Java Integer Cache Pop Quiz
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.
Use J and K for navigation
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