Java Object HashCode Method
Chapter:
Miscellaneous
Last Updated:
02-08-2017 19:34:02 UTC
Program:
/* ............... START ............... */
import java.util.GregorianCalendar;
public class JavahashCodeExample {
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());
}
}
/* ............... END ............... */
Output
Wed Aug 02 23:30:07 IST 2017
261960403
5
5
Notes:
-
The java.lang.Object.hashCode() method returns a hash code value for the object.
- Basically the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. If look into the source of Object class , you will find the following code for the hashCode. public native int hashCode(); It indicates that hashCode is the native implementation which provides the memory address to a certain extent.
Tags
Object HashCode Method, Java, Inheritance