Java包装类与String类的HashCode源码解析

       hashcode()是Object中的方法,因此,任何对象都有hashcode()方法来返回hash值,用以区别其他的对象,在实际生产中,我们一般都会在重写对象的equeals()方法时重写hashcode()方法,那么我们常用的基本数据类型包装类以及String类的hashcode()返回何值?那就让我这个搬运工搬来源码给大伙儿看看~

一,返回结果

在这里插入图片描述
补充:这里可以看到整型数据hashcode相同,但并不equals

二,源码解析

1.整型(Byte,Short,Integer,Long)
整型对象的hashcode即数据本身
(hash值会强转为int型,因此如果Long型大于32位,高位会被截掉)
源码很简单:
Byte
在这里插入图片描述
Short
在这里插入图片描述
Integer
在这里插入图片描述
Long

在这里插入图片描述
2.字符型
Character
在这里插入图片描述
3.浮点型
Float
在这里插入图片描述
Double

在这里插入图片描述
4.布尔型
Boolean
在这里插入图片描述
5.String
在这里插入图片描述
这些源码都是很容易看懂的~下一篇将解析HashMap中哈希值的计算与应用

猜你喜欢

转载自blog.csdn.net/g_y_x_/article/details/83059297