The signature of the hashCode() method is
public int hashCode(){
return x;
}
in this case x must be an int(primitive) but plz can anyone explain it to me that the number
which the hashCode() returns must be a prime number, even number…etc or there is no specification ? the reason behind i am asking this question is i have seen it in different ids the auto generated code always returns a prime number, so i need to know why?
thanks in advance
Actually there is a pretty well-specified contract concerning
hashCode():What you return there is up to you, if you want to, you can just emit
0for every object instance. It might be a bad idea but perfectly valid. It’s by no means always a prime number. As stated in the documentation the default implementation just returns the object’s internal address—this is consistent with the default implementation ofequalschecking for reference equality.Often you can construct the hash from invoking the
hashCodemethod on the fields that make up your object’s state, such as for example:Keep in mind to update both
hashCodeandequals, though, whenever what you consider the representation of the object’s state changes.