Suppose a class MyClass has 3 variables, a,b and c. This class also overrides hashCode() that calculates return value using Joshua Bloch’s approach.
Assuming the above, would it be correct to assume that MyClass() and new MyClass() are deeply equal if their hashcodes match?
For the purposes of the example, assume that both objects are initialized with the same parameters
Certainly not.
hashCodeis required to yield the same value for equivalent objects, but different objects may yield the same value. So, the following is a correct (but inefficient)hashCodeimplementation:Even when you know a specific hashing method is used, it is a bad idea to assume
hashCodeis perfect: later, some refactoring/subclass might replace thehashCodeimplementation with something else. If you want this, create aperfectHashCodemethod that does always return a perfect hashcode, and use that for implementinghashCodeTo answer your initial question: no, even Bloch’s approach afaics does not yield a perfect hashCode, because (because of wrapping over MAXINT) 2 different paths might eventually collide into the same hash code.
This is easy to imagine: say your object holds 2 integers. The hash code is only 1 integer. So it can never yield a different value for each combination of 2 integers.