https://kotlinlang.org logo
#stdlib
Title
# stdlib
r

Rob Elliot

05/28/2022, 7:45 PM
hashCode doesn't necessarily identify a unique object.
It's perfectly valid for two different instances to have the same hashCode.
e

ephemient

05/28/2022, 8:40 PM
the rule is that if two instances equal each other (via
.equals()
, which is
==
in Kotlin (except for NaN which we'll ignore here)), then they must have the same
hashCode()
. this implies the contrapositive (if two instances have different
hashCode()
then they must not be
==
), but does not imply the converse: it is allowed for two instances to have the same
hashCode()
but be
!=
👏 1
😮 2
and of course, as Rob brings up, the same
hashCode()
can be returned by
!==
instances (I'm just clarifying that it must be, for
==
instances)
3 Views