當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java System identityHashCode()用法及代碼示例

Java System 類的 identityHashCode() 方法返回指定對象的哈希碼。它由默認方法 hashCode() 返回。

用法

public static int identityHashCode (Object x)

參數

x- 要為其計算 hashCode 的對象

返回

  1. 返回哈希碼。
  2. 如果哈希碼為空,則返回零作為參考。

例子1

public class SystemIdentityHashCodeExample1{
    public static void main(String[] args) {
        int a=10;
         System.out.println("identity hash code of object a = "+System.identityHashCode(a));
         int b=10;
         System.out.println("identity hash code of object b = "+System.identityHashCode(b));
         int c=01;
         System.out.println("identity hash code of object c = "+System.identityHashCode(c));
    }
}

輸出:

identity hash code of object a = 743673026
identity hash code of object b = 743673026
identity hash code of object c = 557722442

例子2

public class SystemIdentityHashCodeExample2{
    public static void main(String[] args) {
        String a=new String ();
        a.equals("Shubham");
         System.out.println("identity hash code of object a = "+System.identityHashCode(a));
         String b=new String ();
            b.equals("Jadon");
             System.out.println("identity hash code of object b = "+System.identityHashCode(b));	
    }
}

輸出:

identity hash code of object a = 843730481
identity hash code of object b = 743673026





相關用法


注:本文由純淨天空篩選整理自 Java System identityHashCode() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。