How to implement hashCode?
You should implement „hashCode“ so that EQUAL objects return the same hashCode.
Therefore I should take the objects instance variable which is compared for equality in equals method
and call the hashCode() to return an int number which is the same for equal objects.
In this example I calculate the hashCode from the
string instance variable which is turning the balance
for equality of two objects.
My Question is:
- Is this implementation ok?
- I assume „empNum.hashCode( )“ is neccessary to convert a string in an int which is then returned
as „int hashCode() must return an int value.
Would it be neccessary to say „empNum.hashCode( )“ if „empNum“ would already be an int instead
of a String?
- How would I calculate a hashCode if equals() method would compare two instance variables „empNum“ and „name“ instead of one?
C:\Java\EigeneJavaProgramme>
java Employee_Test
false
74113823
74224158
true
74113823
74113823
Appreciate your answers.
Thomas