Java.util.Objects 类

java.util.Objects.hashCode() 方法返回非空参数的哈希码,0 表示空参数。

语法

public static int hashCode(Object obj)

参数

obj 指定一个对象。

返回值

返回非空参数的哈希码,空参数返回 0。

异常

示例:

在下面的示例中,java.util.Objects.hashCode() 方法返回给定对象的哈希码。

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //创建对象
    Object obj[] = {10, 20, 30, 40};

    //打印对象
    System.out.println("The Object is: " + obj);
    System.out.print("obj contains:"); 
    for(Object i: obj)
      System.out.print(" " + i);

    //打印对象的哈希码
    int hcode = Objects.hashCode(obj);
    System.out.println("\nThe hashCode is: " + hcode);
  }
}

上述代码的输出将是:

The Object is: [Ljava.lang.Object;@3af49f1c
obj contains: 10 20 30 40
The hashCode is: 989110044