Open jamesderlin opened 4 years ago
I'm still curious whether identityHashCode
is guaranteed to generate different values for different objects of the same type.
It's not guaranteed to generate different values for different objects.
It can't. There are more different possible objects of class A { final int a, b; A(this.a, this.b); }
than there are integers that identityHashCode
can return. (Well, in practice I guess there won't be that many instance in a single program, but identityHashCode
does not promise to provide unique values. I don't know if it does in practice, but it never promised to.)
It's really a silly sentence to begin with.
It means that if identical(a, b)
then identityHashCode(a) == identityHashCode(b)
, which is ... largely trivial if it behaves like a function.
The one indirect consequence is that the identity hash code is stable over time. That might be worth stating.
So, basically, identityHashCode
assigns a random integer to each object, which is stable over time, and which is not guaranteed to be unique. And Object.hashCode
returns the identityHashCode
of the object.
The last part of the
identityHashCode
documentation says:Exactly what does that mean?
My first interpretation was that the
identityHashCode
values for two objects would be equal if-and-only-if the two objects wereidentical
, but after some empirical observations, it seems thatidentityHashCode
values are equal if two objects areidentical
but that non-identical
objects may have the sameidentityHashCode
. Since theidentityHashCode
of anint
seems to always be itself, a trivial way to generate a collision is:Granted, I probably should have expected this since the function has "hash code" in its name, but I think that the
identityHashCode
documentation could state this explicitly. Compare to the documentation forObject.hashCode
which is much more thorough:(Or maybe there is a 1:1 correspondence with
identical
for objects of the same type? Ultimately what I'd like is the equivalent ofprintf("%p", somePointer)
in C so that I can tell from logging whether two objects separated in time are the same instance.)