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
-
endor56665yOk, what? Why the fuck would it behave differently in the two cases? Could you show a working example of this?
-
Primitives' caching.
Java caches boxed primitives up to some value (I think it's in byte.min and byte.max range) and once allocated references are reused to the same blocks of memory. Since all primitives (also boxed primitives) are immutable it's all goocy. And it makes sense.
And we get this: comparing references to (Integer)65 we are actualy comparing the same reference. Since we are comparing a cached value.
Comparing references between two Integer objects (boxed primitives) of value 666 we are comparing different objects neither of which is cached. Different objects == different references.
Remember that you are comparing references. Not values.
And many people do not know about this cache -
Why does it work this way?
Integers are objects unlike ints. It makes sense that c and d are not the same one. But why a == b?
In Java, the Integer type keeps a cache of all objects with a value in the range of -128 to 127 for performance reasons. So when you declare new variables in that range, you’re actually referring to the same object. -
That's the reason the equals method exists, you wouldn't use == for string comparison either
-
s4nNan1155y@EaZyCode when you're going to use equals, you're going to put a null check as well.
I think most of the Java code spent most of the time checking given variable is null or not :D -
Aren't you using it wrong at the first place? My limited knowledge says if its a non primitive , equate using .equals(...) , No questions asked (ofcourse the class should have equals() defined in it)
-
@StopWastingTime you are correct. And equals is always defined since it originates from java.lang.Object, which is the parent of all the objects.
Some objects simply prefer overriding that method to custom values -
@ZwoRmi That alone wouldn't be unusual at all. Integer is an object, int is not. It's a well known fact that == compares references in Java, so two distinct objects are supposed to not be reference-equal. The quirky part in original code is that two instances of Integer object for 42 are actually equal and the reason is pretty obscure and implementation-dependent.
-
@EaZyCode Any language which has such ambiguous methods of comparison, has a design flaw.
Of course you could claim "any experienced developer knows not to compare values of type x using ==", but why does the language make things so unintuitive in the first place? Why is there a special extra function for comparison where you have to know about references?
The idea that the developer should understand the difference between a value in heap memory and a pointer to such a value could make sense... If we weren't talking about the classic "you don't have to worry about memory"-language, Java.
I think a language should allow types to define what equality means for them, or actually, what any operator means to them.
In languages like Haskell & Rust, you could define colors which could be mixed using +; a card deck in which cards can be compared using < & >; and you could divide a cake object into slices of cake using /. Whatever makes the code more intuitive to the developer. -
Wombat102445y@bittersweet I really have to learn Haskell. But I just don't know what to use it for. 🤷
-
@Wombat For drawing ASCII penises?
I wouldn't learn it for any particular purpose to be honest. If you're bored one weekend, just go through this, program along, fiddle around with the examples:
http://learnyouahaskell.com/chapter...
I've worked professionally on Haskell projects, but employers looking for Haskell devs are rare (outside of Facebook, Microsoft, and a bunch of R&D multinationals).
Learning Haskell is incredibly useful as a mental exercise though.
It becomes much easier to see shortcomings & inconsistencies in the designs of other languages.
When you're used to function purity and a highly perfected type system, you know what to pay extra attention to when you're working with OOP languages.
I've actually started hating PHP less, because Haskell made me aware of patterns to isolate logic in ways which make it easier to test OOP code.
Related Rants
Java begets JavaScript
rant
logically wrong
wk194
technically true