hashCode算法分析String和Integer

String 中hashCode的具体实现方式:

hashCode算法分析String和Integer


看一下创建这个string对象的过程和赋值,String源码中有一个有参数的构造函数:

hashCode算法分析String和Integer

其中  String str = "strsf"; == String str = new String("strsf");

这样我们调用的是String的有参数据构造函数所有  

value 为一个char类型的数组:所以我们可以看作为:

this.value = arg0.value = ['s','t','r','s','f']

其中this.hash 是一个int类型的理解不了,如果有知道怎么理解的或这个上面this.value我理解有问题的欢迎指点。

下面我们分析下这个hashCode的算法

我把this.value看做为不等于0的值

那么this.value.length = 5;

char[] arg1 = ['s','t','r','s','f'];

具体算法:

hashCode算法分析String和Integer

循环拿到arg1数组中每个元素的ascII码对应的十进制的数据,用定制31乘以当前的并加上下一个的ASCII值,返回最后的hashCode。

----------------------------------------------------------------------------------------------------------------------------------

Integer integer = 12333;

hashCode算法分析String和Integer

hashCode算法分析String和Integer

Integer中的hashCode 为值信息。

最后注明:文章中如果有说明或梳理有问题的地方,欢迎各位评论知道,会进一步修改。