Java System类identityHashCode()方法及示例

系统类identityHashCode()方法 (System class identityHashCode() method)

  • identityHashCode() method is available in java.lang package.

    identityHashCode()方法在java.lang包中可用。

  • identityHashCode() method is used to return the hashcode of the given object – By using this method the value of hashcode will be the same as the value of hashcode by using hashCode() method.

    identityHashCode()方法用于返回给定对象的哈希码–通过使用此方法,哈希码的值将与使用hashCode()方法的哈希码的值相同。

  • Let suppose, if we pass an object that holds null value then in that case, the value of hashCode will be 0.

    假设,如果我们传递一个持有null值的对象,则在这种情况下,hashCode的值将为0

  • identityHashCode() method is a static method so this method is accessible with the class name too.

    identityHashCode()方法是静态方法,因此也可以使用类名访问此方法。

  • identityHashCode() method does not throw any exception.

    identityHashCode()方法不会引发任何异常。

Syntax:

句法:

    public static int identityHashCode(Object obj);

Parameter(s):

参数:

  • obj – represent the object for which the hashcode is to be returned.

    obj –代表要为其返回哈希码的对象。

Return value:

返回值:

The return type of this method is int, it returns the hashcode of the given argument.

此方法的返回类型为int ,它返回给定参数的哈希码。

Example:

例:

// 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);
        
    }
}

Output

输出量

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


翻译自: https://www.includehelp.com/java/system-class-identityhashcode-method-with-example.aspx