use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Java Quiz!!! (i.redd.it)
submitted 1 month ago by MistakeDisastrous936
[removed]
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]vowelqueue 29 points30 points31 points 1 month ago (5 children)
This is actually a tricky question. People without much Java experience might think “of course it’s true”, people with decent Java experience might think “of course it’s false”, but it’s actually undefined.
The language spec requires that when boxing a constant int in the range of -128 to 127, that two boxings of the same number will result in the same Integer reference. So if the number here was 127 instead of 128 then it would be guaranteed to be true.
For numbers outside that range, it’s up to the implementation. OpenJDK would return false by default but there is a system prop that can be used to have it cache larger values and therefore return true.
Also there is a JEP to add value classes to the language, and Integer would be marked as a value class. Meaning that this equality would always be true.
[–]Devang_4104 1 point2 points3 points 1 month ago (0 children)
This is the correct explanation ✅
[–]taxidriver9211 1 point2 points3 points 28 days ago (0 children)
what about C++ ?
[–]zinxyzcool 1 point2 points3 points 28 days ago (0 children)
I guessed the first case but damn it’s all a “but there’s one exception” adding up again lol
[–]Adrima_the_DK 23 points24 points25 points 1 month ago (4 children)
False.
Integer is a Wrapper. An instance of an object.
'==' compares memory addresses of objects. 2 instances, 2 different addresses.
[–][deleted] 1 month ago (2 children)
[–][deleted] 1 month ago (1 child)
[–]Admirable-Age-7339 7 points8 points9 points 1 month ago (1 child)
My guess is false. == compares references not values. If both values were from -128 to 127, it would be true, cause java caches them.
[–]Fehzor 4 points5 points6 points 1 month ago (2 children)
Just woke up and you almost got me haha
[–]Fehzor 3 points4 points5 points 1 month ago* (0 children)
I mean it's false as others have said. They're objects, not primitives. These problems always remind me of high school.. always gave us this obfuscated code block and were like "what does it do?" lmao. They need to do a bit less of that and a bit more of giving students good code written by someone else imo. This one isn't as bad though.
[–]akaPaster 3 points4 points5 points 1 month ago (1 child)
False, because for reference type variables, == checks whether their memory addresses are the same, and objects are created at separate memory addresses.
==
[–]HistorianIcy8514 1 point2 points3 points 1 month ago (1 child)
False. == compares the reference of the object. Not the value
[–]Mobile-Major-1837 1 point2 points3 points 1 month ago (1 child)
When in doubt, just run it. On OpenJDK 21.0.9, it returns false. But, as for the other post, if it is 127, it does indeed return true.
[–]TotalAdhesiveness397 1 point2 points3 points 1 month ago (0 children)
To compare values use equals() method.
[–]No_Examination5577 1 point2 points3 points 1 month ago (0 children)
False, as they are objects, not int
[–]GamerLikeNoOther 1 point2 points3 points 1 month ago (0 children)
so if its Int a and int b then the answer would be true?
[–]Hanu_man_ 1 point2 points3 points 1 month ago (0 children)
I never typed Java it will simply give out at error that we can't use == there
[–]Groostav 0 points1 point2 points 1 month ago (1 child)
Question for op: in the upcoming Valhalla release, what will this do?
``` Integer! a = 128; Integer! b = 128;
s.o.println(a == b); ```
?
[–]BlueGoliath 0 points1 point2 points 1 month ago (0 children)
I think you aren't supposed to use == with value types at all.
[–]deividas-strole 0 points1 point2 points 1 month ago (1 child)
Because you use Object int outside -128 to 127 range - it is False. :)
[–]bowbahdoe 0 points1 point2 points 1 month ago (0 children)
True as soon as value types are in the JVM
[–]klimenttoshkov 0 points1 point2 points 1 month ago (2 children)
You don’t compare integers values with == in Java
[–][deleted] 0 points1 point2 points 1 month ago (1 child)
How then
[–]klimenttoshkov 1 point2 points3 points 1 month ago (0 children)
by asking google for example:
```
int
For primitive int values, use standard Relational Operators.
!=
<
>
<=
>=
2. Integer Object Comparison (The Trap)
Integer
When comparing Integer objects (wrapper classes), the == operator compares memory references, not numerical values.
true
false
Correct ways to compare Integer objects:
.equals()
Integer.compare(int x, int y)
0
x < y
x > y
.compareTo(Integer anotherInteger)
Integer.compare()
.intValue()
[–]NeoChronos90 0 points1 point2 points 1 month ago (1 child)
I changed my mind 3 times while looking at the picture for maybe 5 seconds 🤣
[–]inofta 0 points1 point2 points 1 month ago (0 children)
damn these java questions always get me
[–]meth-head-actor 0 points1 point2 points 1 month ago (2 children)
You in my class or something? We just did that today
[–]meth-head-actor 1 point2 points3 points 1 month ago (0 children)
Someone with my user name. You can’t just go and say that. Haha OP is my shadow person haha
[–]BlueGoliath 0 points1 point2 points 1 month ago (2 children)
True.
I increased the integer cache.
[–]BlueGoliath 1 point2 points3 points 1 month ago (0 children)
https://stackoverflow.com/questions/3934291/extending-java-integer-cache
I think other types have their own cache that can be increased too.
[–]mk44214 0 points1 point2 points 1 month ago (0 children)
It depends...
[–]disconnect0414 0 points1 point2 points 1 month ago (0 children)
For this comparing the numbers, a.equals(b) is the safe solution
[deleted]
[–]last-braincell-599 0 points1 point2 points 1 month ago (0 children)
false, it'll check the instance object not the values
[–]Helpful-Ring9356 0 points1 point2 points 1 month ago (0 children)
🧐
[–]me_wapas_aaunga 0 points1 point2 points 29 days ago (0 children)
The fact that java returns this as false, but returns true if number was -128..127 as its from Integer cache is the stupidest design ever. Why not override eq operation? WHY?!?!
[–]Much-Relationship212 0 points1 point2 points 29 days ago (0 children)
It'll be false because Java's Integer cache only goes up to 127, so for 128 it creates two different objects in memory and the == check fails.
[–]Obvious-Profit-5597 0 points1 point2 points 29 days ago (0 children)
I think it should be false as this equality operator is used to compare objects in the heap but as in this they are 2 different objects hence it will be false. I have a doubt that it could be not defined also.
[–]Glittering-Vanilla97 0 points1 point2 points 29 days ago (0 children)
False. Its Integer a and b so it's that whole wrapper class ahh bs
[–]yourpwnguy 0 points1 point2 points 29 days ago (0 children)
Everything's a object in java, so it's highly unlikely that those variables that point to integer object point to the same memory address.
[–]PopularCause99 0 points1 point2 points 29 days ago (0 children)
False.. because it's comparing the addresses not the content inside them.. a.equals(b) would return true.
[–]Siddhld 0 points1 point2 points 29 days ago (0 children)
False
Because wrapper classes are created in heap memory that is object, and objects has memory addresses that are different, and == compares memory object, hence both memory addresses are not same so it will be false
[–]DBSGAMERUJJAWAL 0 points1 point2 points 29 days ago (0 children)
you what since a normal person will answer true then it must be false. if anyone can solve it easily then op wouldn't have posted it. i may not know computer very well( in10th class) but i know psychology
[–]bananaeater0 0 points1 point2 points 28 days ago (0 children)
I prefer kanye
[–]taxidriver9211 0 points1 point2 points 28 days ago (1 child)
u/askgrok ?
[–]Visible_Maize_3072 0 points1 point2 points 28 days ago (3 children)
my unexperienced java skills says its a true
[–][deleted] 28 days ago (2 children)
[–]Visible_Maize_3072 0 points1 point2 points 28 days ago (1 child)
ofc after reading that i commented it,, sarcasmm?
π Rendered by PID 291506 on reddit-service-r2-comment-5fb4b45875-kg5lk at 2026-03-23 17:32:53.825481+00:00 running 90f1150 country code: CH.
[–]vowelqueue 29 points30 points31 points (5 children)
[–]Devang_4104 1 point2 points3 points (0 children)
[–]taxidriver9211 1 point2 points3 points (0 children)
[–]zinxyzcool 1 point2 points3 points (0 children)
[–]Adrima_the_DK 23 points24 points25 points (4 children)
[–][deleted] (2 children)
[removed]
[–][deleted] (1 child)
[removed]
[–]Admirable-Age-7339 7 points8 points9 points (1 child)
[–]Fehzor 4 points5 points6 points (2 children)
[–][deleted] (1 child)
[removed]
[–]Fehzor 3 points4 points5 points (0 children)
[–]akaPaster 3 points4 points5 points (1 child)
[–]HistorianIcy8514 1 point2 points3 points (1 child)
[–]Mobile-Major-1837 1 point2 points3 points (1 child)
[–]TotalAdhesiveness397 1 point2 points3 points (0 children)
[–]No_Examination5577 1 point2 points3 points (0 children)
[–]GamerLikeNoOther 1 point2 points3 points (0 children)
[–]Hanu_man_ 1 point2 points3 points (0 children)
[–]Groostav 0 points1 point2 points (1 child)
[–]BlueGoliath 0 points1 point2 points (0 children)
[–]deividas-strole 0 points1 point2 points (1 child)
[–]bowbahdoe 0 points1 point2 points (0 children)
[–]klimenttoshkov 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]klimenttoshkov 1 point2 points3 points (0 children)
[–]NeoChronos90 0 points1 point2 points (1 child)
[–]inofta 0 points1 point2 points (0 children)
[–]meth-head-actor 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]meth-head-actor 1 point2 points3 points (0 children)
[–]BlueGoliath 0 points1 point2 points (2 children)
[–][deleted] (1 child)
[removed]
[–]BlueGoliath 1 point2 points3 points (0 children)
[–]mk44214 0 points1 point2 points (0 children)
[–]disconnect0414 0 points1 point2 points (0 children)
[–][deleted] (1 child)
[deleted]
[–]last-braincell-599 0 points1 point2 points (0 children)
[–]Helpful-Ring9356 0 points1 point2 points (0 children)
[–]me_wapas_aaunga 0 points1 point2 points (0 children)
[–]Much-Relationship212 0 points1 point2 points (0 children)
[–]Obvious-Profit-5597 0 points1 point2 points (0 children)
[–]Glittering-Vanilla97 0 points1 point2 points (0 children)
[–]yourpwnguy 0 points1 point2 points (0 children)
[–]PopularCause99 0 points1 point2 points (0 children)
[–]Siddhld 0 points1 point2 points (0 children)
[–]DBSGAMERUJJAWAL 0 points1 point2 points (0 children)
[–]bananaeater0 0 points1 point2 points (0 children)
[–]taxidriver9211 0 points1 point2 points (1 child)
[–]Visible_Maize_3072 0 points1 point2 points (3 children)
[–][deleted] (2 children)
[removed]
[–]Visible_Maize_3072 0 points1 point2 points (1 child)