String#hashCode:
private int hash;...public int hashCode() { int h = hash; if (h == 0 && count > 0) { int off = offset; char val[] = value; int len = count; for (int i = 0; i < len; i++) { h = 31*h + val[off++]; } hash = h; } return h;}由于
String永不更改的内容,因此该类的创建者选择在对哈希进行一次计算之后就对其进行缓存。这样,不会浪费时间重新计算相同的值。



