6
Ni-Q
2y

Just stumbled upon this question and it says the output is:

TRUE
FALSE

… but why? 😱

Comments
  • 0
    Fuck if I know. Probably some java magic. Just use the primitves
  • 21
    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
  • 5
    @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
  • 8
    @12bitfloat "makes very little sense" is very diplomatic.

    i'd rather say "it's fucking ludicrous"
  • 1
    @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
  • 1
    @tosensei Thanks 🙏
    Black magic.
  • 5
    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?
  • 4
    @nitwhiz That's what I'm saying! You do a dumb operation you get a dumb result
  • 5
    @nitwhiz i think the boxed primitive classes are mainly there for generics

    Can't have List<int>, so List<Integer> "solves" the issue
  • 0
    i1 and i2 are different onjects why is i1==i2 true?
  • 3
    @nitwhiz Agreed. Looks dumb to me, too. Just wanted to know why somebody came up with this while it most incredibly works. Wouldn’t normally write it that way though
  • 4
    @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"
  • 4
    @TeachMeCode because, to quote myself:

    "but -128 to 127 are cached for some weird java-reason, so you got two equal `100`-objects."
  • 3
    Came 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?
  • 3
    @Root if the only alternative is python, javascript, or another bottom-tier-garbage-language.
  • 3
    @tosensei if Java is an option, then Kotlin is an option, too. So, there is indeed never a good reason to use Java.
  • 1
    @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...
  • 3
    Yes I too love putting my 32bit integer onto the heap and then use a 64bit pointer to reference it

    Absolutely amazing design
  • 0
  • 1
    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.
Add Comment