aspose file tools*
The moose likes Mock Exam Errata and the fly likes SJCP - equals() question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "SJCP - equals() question" Watch "SJCP - equals() question" New topic
Author

SJCP - equals() question

Colin Hillman
Greenhorn

Joined: May 27, 2008
Posts: 2
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 ]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
Welcome to JavaRanch.

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.
Colin Hillman
Greenhorn

Joined: May 27, 2008
Posts: 2
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?
Cyril Boulm´┐Ż
Greenhorn

Joined: May 15, 2008
Posts: 11
I have the same problem,

For me :

break the hashCode() contract.

Error in exam?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
You will have to run some test code, but if you can find a case where you can show a.equals(b) && a.hashCode() != b.hashCode() you have demonstrated that the class breaches the contract for hashCode.

Please try it and report.

Yes, it does appear that you probably both have the same problem, obviously with the same question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: SJCP - equals() question
 
Similar Threads
Equals method logic incorrect on Master Exam
Possible Wrong answer in k&b Master Exam
hashcode contract S&B question
mistake in k&b hashCode()/equals() contract question
hashcode & equals question