This is the mail archive of the
mailing list for the Java project.
Re: String hashCode
> I'd prefer to simply add a hashcode field, but only set it if hashcode() is
> called or the string gets interned. But, as you point out, there is the
> problem of how to determine whether the value has been set or not.
> It would seem reasonable to set it to null or some other unlikely value
> initially, then bump it if the string just happens to hash to the same value
> - but this would violate the published spec of String.hashCode.
A little variation: you could set 0 as a not-yet-calculated indicator, and
on the unlikely event the hashCode ends up 0, don't bump anything, just
return 0 and recalculate it next time...