Just curious, in String's hashCode implementation what is the reason behind extra references creation in a hashCode implementation (v 1.8.0_65):
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
Taking into consideration that value
is final and created in constructor only (i.e. threadsafe) why do we need variable val[] reference here?
I.e. will this work:
public int hashCode() {
if (hash == 0 && value.length > 0) {
int h = 0;
for (int i = 0; i < value.length; i++) {
h = 31 * h + value[i];
}
hash = h;
}
return hash;
}
?
In addition to copying values from heap to stack to speed things up it is also about race conditions described in comments by @zapl. Which was not obvious to me before his comment.
Yes, it is possible for two Strings to have the same hashcode - If you take a look at the Wikipedia article, you will see that both "FB" and "Ea" have the same hashcode. There is nothing in the method contract saying a hashCode() should be used to compare for equality, you want to use equals() for that.
Threads are different and hash codes are the same. it is possible that two instances can give the same hash code, but this is unlikely.
They are not unique. Show activity on this post. By doing it's own hashing of the key String, that code risks the chance that two different key strings will generate the same integer map key and the code will fail in some situations. In general, the code should probably be using Map<String,String> .
When two unequal objects have the same hash value, this causes a collision in the hash table, because both objects want to be in the same slot (sometimes called a bucket).
It seems like the intent is to put hash
and the value
handle explicitly on the stack
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With