Does hashcode number represent the memory address? [duplicate]

Solution 1:

Hashcode is not a unique identification. It's just a number that helps you distinguish objects. Two different objects may have the same hash code and it is fine.

HashCode characteristics:

  1. If obj1 and obj2 are equal, they must have the same hash code.
  2. If obj1 and obj2 have the same hash code, they do not have to be equal.

Solution 2:

If Employee class hasn't overridden the hashCode() method , then it will use the one defined in its super class, probably Object class . hashCode() in Object class says ,

As much as is reasonably practical, the hashCode method defined by class Object
does return distinct integers for distinct objects. (This is typically 
implemented by converting the internal address of the object into an integer, 
but this implementation technique is not required by the JavaTM 
programming language.)

So, in short it may or may not depending upon the implementation.Suppose, if Employee class has overridden the hashCode() as(though bad practice and useless) :

public int hashCode() {
   return 10;
}

Then, you can see that it doesn't return a memory address here.

Solution 3:

Not necessarily the memory address. It should be kept different for different objects. But it might be anything. You can also override default hashCode definition with your own.

Solution 4:

Hashcode is a number used by JVM for hashing in order to store and retrieve the objects. For example, when we add an object in hashmap, JVM looks for the hashcode implentation to decide where to put the object in memory. When we retrieve an object again hashcode is used to get the location of the object. Note that hashcode is not the actual memory address but its a link for JVM to fetch the object from a specified location with a complexity of O(1).

Solution 5:

Simple Answer NO

A hashcode is an integer value that represents the state of the object upon which it was called. That is why an Integer that is set to 1 will return a hashcode of "1" because an Integer's hashcode and its value are the same thing. A character's hashcode is equal to it's ASCII character code. If you write a custom type you are responsible for creating a good hashCode implementation that will best represent the state of the current instance.

So for your class you can implement the hashcode Method and return whatever you want.