Java对象的默认hashCode()实现 [英] Default hashCode() implementation for Java Objects
问题描述
我试图理解Java的Object的hashCode(),并看到了Java Object的hashCode()方法的以下代码:
I was trying to understand the hashCode() for Java's Object and saw the following code for Java Object's hashCode() method:
package java.lang;
public class Object {
// Some more code
public native int hashCode();
// Some other code
}
现在,我们知道如果我们创建一个类,它会隐式扩展Object类,为此,我写了一个示例:
Now, we know that if we create a class it implicitly extends the Object class, and to do this I wrote a sample example:
package com.example.entity;
public class FirstClass {
private int id;
private String name;
// getters and setters
}
所以,这个类即: FirstClass
将隐式扩展 Object
类。
So, this class viz: FirstClass
would be extending the Object
class implicitly.
package com.example.app.main;
import com.example.entity.FirstClass;
public class MainApp {
public static void main(String[] args) {
FirstClass fs = new FirstClass();
fs.setId(1);
fs.setName("TEST");
System.out.println("The hasCode for object fs is " + fs.hashCode());
}
}
As FirstClass
隐式扩展 Object
类,因此它将具有 Object
classes' hashCode ()
方法。
As FirstClass
is extending Object
class implicitly, hence it would have Object
classes' hashCode()
method.
我在<$ c $上调用 hashCode()
c> FirstClass 对象,因为我没有覆盖 hashCode()
,理论上它应该调用 Object
class的 hashCode()
。
I invoked the hashCode()
on FirstClass
object , and as I haven't overridden the hashCode()
, by theory it should invoke Object
class's hashCode()
.
由于Object类没有任何实现,它如何计算任何对象的哈希码?
As Object class don't have any implementation, how is it able to calculate the hash-code for any object?
在我的情况下,当我运行程序时,它返回的哈希码是366712642.。
In my case, when I run the program the hash-code which it returned was 366712642.
任何人都可以帮我理解这个吗?
Can anyone help me understand this?
推荐答案
尽管这里有一些答案表明默认实现是基于内存的,但这是完全错误的。很多年来情况并非如此。
Even though there are some answers here stating that the default implementation is "memory" based, this is plain wrong. This is not the case for a lot of years now.
在java-8下,你可以这样做:
Under java-8, you can do :
java -XX:+PrintFlagsFinal | grep hashCode
获取使用的确切算法( 5
默认)。
To get the exact algorithm that is used (5
being default).
0 == Lehmer random number generator,
1 == "somehow" based on memory address
2 == always 1
3 == increment counter
4 == memory based again ("somehow")
5 == read below
默认情况下( 5
),它使用Marsaglia XOR-Shift算法,无关记忆。
By default (5
), it is using Marsaglia XOR-Shift algorithm, that has nothing to do with memory.
如果你这样做,这不是很难证明:
This is not very hard to prove, if you do:
System.out.println(new Object().hashCode());
多次,在一个新的VM中 - 你会得到相同的值,所以Marsaglia XOR -Shift从种子开始(总是相同,除非其他一些代码不改变它)并从中起作用。
multiple times, in a new VM all the time - you will get the same value, so Marsaglia XOR-Shift starts with a seed (always the same, unless some other code does not alter it) and works from that.
但即使你切换到一些hashCode,基于内存,和对象可能四处移动(垃圾收集器调用),如何确保在 GC移动此对象后采取相同的hashCode?提示:indentityHashCode和Object标头。
But even if you switch to some hashCode that is memory based, and Objects potentially move around (Garbage Collector calls), how do you ensure that the same hashCode is taken after GC has moved this object? Hint: indentityHashCode and Object headers.
这篇关于Java对象的默认hashCode()实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!