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


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

System類identityHashCode()方法

  • identityHashCode() 方法可在java.lang包。
  • identityHashCode() 方法用於返回給定對象的哈希碼 - 通過使用此方法,哈希碼的值將與使用 hashCode() 方法的哈希碼的值相同。
  • 假設,如果我們傳遞一個持有空值的對象,那麽在這種情況下,hashCode 的值將為 0。
  • identityHashCode() 方法是一個靜態方法,所以這個方法也可以用類名訪問。
  • identityHashCode() 方法不拋出任何異常。

用法:

    public static int identityHashCode(Object obj);

參數:

  • obj– 表示要為其返回哈希碼的對象。

返回值:

這個方法的返回類型是int,它返回給定參數的哈希碼。

例:

// Java program to demonstrate the example of 
// identityHashCode () method of System Class

import java.lang.*;
import java.io.*;

public class IdentityHashCodeMethod {
    public static void main(String[] args) throws Exception {

        File file1 = new File("Java");
        File file2 = new File("Programming");

        // getting hashcode
        int hcode1 = System.identityHashCode(file1);
        System.out.println(hcode1);

        // getting hashcode
        int hcode2 = System.identityHashCode(file2);
        System.out.println(hcode2);
        
    }
}

輸出

E:\Programs>javac IdentityHashCodeMethod.java
E:\Programs>java IdentityHashCodeMethod
1018081122
242131142


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java System class identityHashCode() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。