2

When I use the hashCode() method in the following statement:

System.out.println(obj.getClass().getName() + "@" +
                  Integer.toHexString(System.identityHashCode(obj))); 

I get output like this:

"Contact@29453f441"

Which is unique for each individual object. What would the effect of overriding hashCode() be?

clearlight
  • 12,255
  • 11
  • 57
  • 75
xerox007
  • 54
  • 1
  • 9

2 Answers2

3

You would lose any performance given by an hashmap, that can retrieve items from a collection in O(1) time for objects with different hashes, which is what we want to achieve when using HashMaps.

Here is a quote from another question:

When two unequal objects have the same hash value, this causes a collision in the hash table, because both objects want to be in the same slot (sometimes called a bucket).

If you do not use hashmap or other algorithms that depend on an object's hashcode it causes no problems.

As for comparisons they will be differentiated thorugh equals().

Edu G
  • 1,030
  • 9
  • 23
-1

From docs of System class

public static int identityHashCode(Object x)

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.

So even if the hashcode() is overridden, it should not effect it.

Krishna
  • 88
  • 7