Just wondering, in the implementation of the String hashCode, there is a reason for creating additional links in the implementation of hashCode (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; }
Given that value is final and only created in the constructor (i.e., thread safe), why do we need the variable val [] here?
those. 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 the heap to the stack to speed things up, this also applies to the race conditions described in the @zapl comments. This was not obvious to me before his comment.
source share