Java System类identityHashCode()方法及示例
系统类identityHashCode()方法
identityHashCode()方法在java.lang包中可用。
identityHashCode()方法用于返回给定对象的哈希码–通过使用此方法,哈希码的值将与使用hashCode()方法的哈希码的值相同。
假设,如果我们传递一个持有null值的对象,则在这种情况下,hashCode的值将为0。
identityHashCode()方法是静态方法,因此也可以使用类名访问此方法。
identityHashCode()方法不会引发任何异常。
语法:
public static int identityHashCode(Object obj);
参数:
obj–代表要为其返回哈希码的对象。
返回值:
此方法的返回类型为int,它返回给定参数的哈希码。
示例
//Java程序演示的例子
//系统类的identityHashCode()方法
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");
//获取哈希码
int hcode1 = System.identityHashCode(file1);
System.out.println(hcode1);
//获取哈希码
int hcode2 = System.identityHashCode(file2);
System.out.println(hcode2);
}
}输出结果
E:\Programs>javac IdentityHashCodeMethod.java E:\Programs>java IdentityHashCodeMethod 1018081122 242131142