2016-08-19 44 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

回答

相關問題