Java.lang.Object.hashCode() Method



Description

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

Declaration

Following is the declaration for java.lang.Object.hashCode() method

public int hashCode()

Parameters

NA

Return Value

This method returns a hash code value for this object.

Exception

NA

Example

The following example shows the usage of lang.Object.hashCode() method.

package com.tutorialspoint;

import java.util.GregorianCalendar;

public class ObjectDemo {

   public static void main(String[] args) {

      // create a new ObjectDemo object
      GregorianCalendar cal = new GregorianCalendar();

      // print current time
      System.out.println("" + cal.getTime());

      // print a hashcode for cal
      System.out.println("" + cal.hashCode());

      // create a new Integer
      Integer i = new Integer(5);

      // print i
      System.out.println("" + i);

      // print hashcode for i
      System.out.println("" + i.hashCode());
   }
}

Let us compile and run the above program, this will produce the following result −

Sat Sep 22 00:35:34 EEST 2012
-458465658
5
5
java_lang_object.htm
Advertisements