这两个方法都是用于计算标识哈希码,不过性能不一样,HashCode()性能好一些,通常使用的是这个方法,不过HashCode()与identityHashCode()计算出来的标识哈希码值有时候是不一样的,这取决于HashCode()是否有被重写,identityHashCode()是根据Object类hashCode()方法来计算hash值的,所以无论是否重写HashCode(),值都是不会变的。
下面一个小例子就可以体现出来。
public class Test2 {
public static void main(String[] args) {
duck duck = new duck();
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> list2 = new ArrayList<>();
System.out.println(System.identityHashCode(list));
System.out.println(System.identityHashCode(list2));
System.out.println(duck.hashCode());
System.out.println(System.identityHashCode(duck));
}
}
class duck{
}