hashCode()是如何用Java计算的 [英] How is hashCode() calculated in Java
问题描述
什么值 hashCode()
方法在java中返回?
What value is hashCode()
method is returning in java?
我读到它是一个内存对象的引用...当我为打印哈希值时,新的整数(1)
它是1; for String(a)
是97.
I read that it is an memory reference of an object... When I print hash value for new Integer(1)
it is 1; for String("a")
is 97.
我很困惑:是ASCII还是什么类型的值是什么?
I am confused: is it ASCII or what type of value is?
推荐答案
哈希码是一个整数值,表示调用它的对象的状态。这就是设置为1的 Integer
将返回哈希码1的原因,因为 Integer的
哈希码及其价值是一回事。字符的哈希码等于它的ASCII字符代码。如果你编写一个自定义类型,你负责创建一个好的 hashCode
实现,它最能代表当前实例的状态。
A hashcode is an integer value that represents the state of the object upon which it was called. That is why an Integer
that is set to 1 will return a hashcode of "1" because an Integer's
hashcode and its value are the same thing. A character's hashcode is equal to it's ASCII character code. If you write a custom type you are responsible for creating a good hashCode
implementation that will best represent the state of the current instance.
这篇关于hashCode()是如何用Java计算的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!