what is the point of the prime number anyway? 31 is a ODD-PRIME number and to choose an Odd number prevents multiplication over-flow and prime is a traditional approach.
I think this part can be understand in more detailed manner from here
Why should we assign the null reference to 31
((id == null) ? 0 : id.hashCode())
Here we are checking if id has a non-null value or not. If there is some non-null value then it must be having some hashcode and we are adding that hashcode in our object's hashcode.
So this way your hashcode for two object will differ if id for those two object differs
.