Yahoo Interview Question
Software Engineer / Developershm.. the question wants relation between hashcode method and equals method.
if you made a user defined class, you should override hashcode and equals methods satisfying codes below.
{{
UserDefindedClass a, b;
a = make();
b = anotherMake();
if (a.equals(b)) {
assertTrue(a.hashCode()==b.hashCode());
}
}}
One needs to override hashCode() and equals() method when one needs to use a custom class as a key into HashMap (or Hashtable). following rules apply
a) Same members must be used in both hashCode() and equals().
b) hashCode() must different values as possible for different objects, this will ensure objects are better placed in different buckets of the map.
c) If one uses different members within hashCode and equals method then when map.get(o1) will return null even after we have zeroed in on the bucket.
hashCode() is a native method which returns the address at which this object is stored in heap.hashCode() in Object returns the hexadecimal representation of the address at which this object is stored in heap. toString() returns classname + "@" + hashCode().
Hashcode of Object class returns a value by converting the internal address of the object into an integer,
- sdm February 28, 2010