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;
    }求解:
这个是string类内部的源代码,当字符串无限长的时候,这个for循环计算得到hashCode不会越界么?

解决方案 »

  1.   

    溢出就溢出呗,hashcode最终效果是得到一个值用来标识对象的,溢出为负不影响这种效果
      

  2.   

    你是对那个循环中的len 有疑问是么?
      那个是外部的count决定的,也就是hashCode这个方法在进行hash值比较的时候,长度是可以变化的。
      

  3.   

    count在源代码里有注释啊。就是字符串的长度。。