On Apr 1, 2019, at 12:50 PM, dean.l...@oracle.com wrote: > > Wouldn't it be better to write a non-0 value when the computed hash code is > 0, so we don't have to recompute it? Is there some advantage to writing 0 > instead of any other value, such as 1?
Zero is the easiest sentinel value because it's the default value for int. *Any* 32-bit sentinel you choose will be the legitimate hashCode of *some* string. That is to say, the range of String.hashCode is all 2^32 points of the int domain. That's why we need an extra bit somewhere if we want to distinguish a sentinel (like zero) from an identical legitimate hash code that has been cached. Luckily, there is plenty of "slack" in the memory layout of String. We can find a bit, if we want to go there. — John