Java Utility Library

Java Objects - hashCode() Method



The java.util.Objects.hashCode() method returns the hash code of a non-null argument and 0 for a null argument.

Syntax

public static int hashCode(Object obj)

Parameters

obj Specify an object.

Return Value

Returns the hash code of a non-null argument and 0 for a null argument.

Exception

NA

Example:

In the example below, the java.util.Objects.hashCode() method returns the hash code of the given object.

import java.util.*;

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

    //printing the object
    System.out.println("The Object is: " + obj);
    System.out.print("obj contains:"); 
    for(Object i: obj)
      System.out.print(" " + i);

    //printing the hashcode of the object
    int hcode = Objects.hashCode(obj);
    System.out.println("\nThe hashCode is: " + hcode);
  }
}

The output of the above code will be:

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

❮ Java.util - Objects