Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
did it really happen?
did the people unable to use a SEARCH ENGINE now infect devrant, too? https://stackoverflow.com/questions...
TL;DR: in java, everything is an object. so you got two _different_ `1000`-objects. but -128 to 127 are cached for some weird java-reason, so you got two equal `100`-objects.
in conclusion: JS - java sucks -
@tosensei This is it. Though to be fair, checking reference equality for immutable types like Integer makes very little sense in the first place. One operation on them and you have a new object anyway
-
-
@tosensei Well it's a idiosyncrasy of the language that == checks reference equality, which in this case is a mostly pointless operation
Integer.equals() is what he actually wants -
What good reason is there to use Integer over int as type?
Let me ask this differently: Why would you do things weird and expect them to work? -
ess3sq1632y@nitwhiz i think the boxed primitive classes are mainly there for generics
Can't have List<int>, so List<Integer> "solves" the issue -
@TeachMeCode Because Java interns boxed ints in a given range, meaning all Integer instances of a given range are cached globally
Same thing happens with String literals: "abc" == "abc" will always be true since the compiler interns the string "abc" -
@TeachMeCode because, to quote myself:
"but -128 to 127 are cached for some weird java-reason, so you got two equal `100`-objects." -
Root825572yCame here to say the same thing as @tosensei, but since he’s already done that I’ll just say something snarky instead:
Is there ever a good reason to use Java? -
@Root if the only alternative is python, javascript, or another bottom-tier-garbage-language.
-
@tosensei if Java is an option, then Kotlin is an option, too. So, there is indeed never a good reason to use Java.
-
@12bitfloat people wonder why Java has a high memory usage meme attached, then Java goes ahead and just caches the fucking string "abc" for some reason...
-
Yes I too love putting my 32bit integer onto the heap and then use a 64bit pointer to reference it
Absolutely amazing design -
oh wow, so now i know why you're supposed to be using .Equals(), as well as why i will never use java.
what a fucking retarded joke of a language.
Related Rants
Just stumbled upon this question and it says the output is:
TRUE
FALSE
… but why? 😱
question
java
beginner