2016-08-19 53 views
-3

我有一个employee2 java文件,我创建了3个对象,我试图计算这些对象的哈希码()使用System.identityhashCode(Object o) ,但它始终是为所有对象给出相同的值,这怎么可能System.identityHashCode()给不同的对象相同的值

注 - >我还没有实现hashCode()方法或等于,我只是用hashCode()方法

代码的默认实现如下:

public class employee2 
    { 
    private int empid; 
    private String name; 
    private String dept; 

    employee2(){ 

    } 

    public employee2(int empid,String name,String dept){ 
    this.empid=empid; 
    this.name=name; 
    this.dept=dept; 
    } 

    public static void main(String args[]){ 
     employee2 e1=new employee2(9846,"Amol Sngh","Science"); 
     employee2 e2=new employee2(9844,"Amol Singh","Scienc"); 
     employee2 e3=new employee2(98446,"Amol Singh","Science"); 
     employee2 e4=new employee2(96,"Amol Sh","Sciece"); 

     System.out.println(System.identityHashCode(e1)+" "+System.identityHashCode(e2)+" "+System.identityHashCode(e3)+" "+System.identityHashCode(e4)); 

    }  
} 

它总是给予价值为每个对象

+1

我刚跑了你的代码,它共有4个款不同的值:'1922154895','883049899','2093176254', '1854731462'。 – DimaSan

+0

不可能重现。您可能运行了旧版本的代码。重新编译并重新运行。 –

+0

你的意思是什么旧版本? – DimaSan

回答

相关问题