Java中的hashCode()是如何计算的 [英] How is hashCode() calculated in Java
问题描述
java中hashCode()
方法返回什么值?
What value does the hashCode()
method return in java?
我读到它是一个对象的内存引用...new Integer(1)
的哈希值为 1;String("a")
的哈希值为 97.
I read that it is a memory reference of an object... The hash value for new Integer(1)
is 1; the hash value for String("a")
is 97.
我很困惑:它是 ASCII 还是什么类型的值?
I am confused: is it ASCII or what type of value is?
推荐答案
哈希码是一个整数值,表示调用它的对象的状态.这就是为什么设置为 1 的 Integer
将返回1"的哈希码,因为 Integer's
哈希码和它的值是相同的.一个字符的哈希码等于它的 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.
这篇关于Java中的hashCode()是如何计算的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!