什么是变量" offset" &安培; "散列"在String.hashCode()中?

时间:2016-07-23 21:33:52

标签: java

我已经读过这是String.hashCode()

的源代码
   public int hashCode() {
   int h = hash;
   if (h == 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;
   }

我的问题是,&#34;偏移&#34; &安培; &#34;散列&#34 ;?我可以说&#34;价值&#34;是实际的字符串和&#34;长度&#34;是它的长度,但我不明白其他两个是什么。

1 个答案:

答案 0 :(得分:0)

偏移量是String的值[]数组的第一个索引。 散列变量是用于缓存hashCode的字段,以便更有效(hashCode()方法仅在其当前值为0时计算散列;否则返回缓存散列)

相关问题