Java Utility Library

Java Object - hashCode() Method



The java.util.Object.hashCode() method returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.

Syntax

public int hashCode()

Parameters

No parameter is required.

Return Value

Returns a hash code value for this object.

Exception

NA

Example:

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

import java.lang.*;

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 = obj.hashCode();
    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.lang - Object