Only recently, I discovered that an empty String has hash code zero. This is surprising to me because null is normally assigned hash code zero, e.g., Objects.hashCode(Object) and ArrayList.hashCode().
Here is the JDK 11 source code for String.hashCode():
/** Cache the hash code for the string */
private int hash; // Default to 0
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
hash = h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
}
return h;
}
Idea: An empty String could have hash code one because that would match Arrays.hashCode(Object[]) for empty arrays. Alternatively, any other hard-coded, non-zero value could be used, similar to serialVersionUID. The purpose would be to distinguish from null. If this idea is flawed (except for backwards compatibility concerns), please kindly explain why.
I found other questions/answers that approach the issue... but none answer exactly: