当前位置: 首页>>编程示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。