One of the mock questions in the MasterExam allows these two implementations of equals() (I've commented out the first)
I agree with the commented out version, but aren't there values for rate and bal which would break the hashcode contract for the uncommented version - if rate=1 and bal=2 in one object and rate=2 and bal=1 in the other? That way, equals would return true but hashcode false, assuming code has a non-empty string - or am I missing something?
Does anyone know if errata has been produced for MasterExams (from the Sierra and Bates CDROM)? [ May 31, 2008: Message edited by: Colin Hillman ]
Just hving a quick look at those equals() methods, I think you are correct. 6 and 4 or 4 and 6 would create such errors. That method would appear capable of returning true from two SortOfs which do not encapsulate the same data.
Joined: May 27, 2008
Thanks - yes you're right, although it might be a case of "equality is in the eye of the beholder" - i.e. we can define equality on any terms.
My concern is that it breaks the hashcode() contract since in the example you gave, x.equals(y) is true, but x.hashcode() != y.hashcode(). Am I right?