File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Equals and Hash Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Equals and Hash Code" Watch "Equals and Hash Code" New topic
Author

Equals and Hash Code

Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

I have put up an article explaining details of these two methods at - Equals and Hash Code. It also includes a small mock test covering this topic. It would be great if you could give me the feedback abt this article and the test. If there are any errors in it, please do let me know. I'll corect them.
rgds,
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Good work Manish
Here are my comments to the first part (the equals method). I will finish the article a little bit later. My propositions may be vetoed anytime if somebody feels I didn't get it right
"when objects of such class are added" should read
"when objects of such classes are added"
"The contract of the equals precisely" should read
"The contract of the equals method precisely" or
"The contract of equals() precisely"
"two object, and consequently" should read
"two objects, and consequently"
"if first object is equal to second object and second object is" should read
" if the first object is equal to the second object and the second object is"
"and follow the symmetry principle; one of them can not" should read
"and follow the symmetry principle, one of them can not"
"All three must agree and and follow" should read
"All three must agree and follow"
" A and B both implement equals method" should read
" A and B both implement the equals method"
"if they are not equal, they they must remain" should read
"if they are not equal, they must remain"
"that you check equals method argument" shold read
"that you check the equals method argument"
"Because, JLS 15.20.2 specifies" it would be nice to include the link to that JLS section.
"that if left hand side" should read
"that if the left hand side"
"on right hand side." should read
"on the right hand side RHS."
"must have same hash code" should read
"must have the same hash code"
I would rephrase "however converse is NOT true" to
"however the opposite is NOT true"
[ September 19, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Thanks a billion Valentin!!!
I'll make those changes, this is the first draft I'll complete it by this weekend.
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Fair enough
Keep it up...
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

I mean, I intend to add few more things to this article, but it'll take some time to do them. So I intend to complete the article by this weekend.
I have already made the changes you have mentioned!
- Manish
Niral Trivedi
Ranch Hand

Joined: Nov 26, 2001
Posts: 46
Good work manish..
really helpful to understand the concepts..
Thanks.
Niral
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
My feedback (continued)
"This is what JDK 1.4 API documentation" should read
"This is what the JDK 1.4 API documentation"
"must be consistently same" should read
"must be consistently the same"
"However, third point elaborates" should read
"However, the third point elaborates"
"must produce same hash code as long" should read
"must produce the same hash code as long"
"Rest of the requirements" should read
"The rest of the requirements"
"for the objects of same class," should read
"for objects of the same class,"
"similar to built in Java classes", should read
"similar to built-in Java classes"
First code example, line 10: what if the argument object "obj" is an instance of a subclass of test? As stated in the book Java Rules by Douglas Dunn (pp. 276-282), I would write the following on line 10:
if(obj.getClass() != this.getClass())
You might want to update the rest of your article to reflect this.
Note: this is a very important mistake everybody does and that should be taken seriously.
The author states that writing
if(!(obj instanceof Test))
is only a performance optimization, but a violation of the equals contract, since subclasses of Test may not return false.
"that invoking equals method" should read
"that invoking the equals method"
"Do not change type of the" should read
"Do not change the type of the"
"values for same invocation and" should read
"values for different invocations and"
"in the calculation of hash code value" should read
"in the calculation of the hash code value"
"for implementing hashCode method" should read
"for implementing a hashCode method"
[ September 19, 2002: Message edited by: Valentin Crettaz ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Manish:
My browser froze when I attemted it earlier today....
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
It again did. First some error in line 148 and then (probably) an advertisement that your computer is sending IP address and that window freezes...
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Barkat Mardhani:
It again did. First some error in line 148 and then (probably) an advertisement that your computer is sending IP address and that window freezes...

Hi Barkat,
I have tested this page on IE 6, 5, 5.5, Netscape 4.7, Netscape 6.1 and Opera 5.12 and there is no problem. Altough, I have tested it only on Windows (2k, and 98). Which operating system & bowser u r using? As for the advertisement, the host (geocities.com) puts it, I have no control over it. It is usually displayed in the right hand top corner in a layer, but you can click "X" to close it.
I have made few changes in the article and have added 2 more questions in it, pls try again and let me know if u are still having problems.
- Manish
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Before I try, I wanted to let you know that I am using Win 98 and browser provided by compuserv....
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
guess what...I worked this time. I will give my feedback after I am done reading....
Sun LiWei
Ranch Hand

Joined: Aug 10, 2002
Posts: 49
I can't visit www.geocities.com,coz I come from China,And I don't know a proxy server too.Could someone do me a favor,email the article and the mock test ??Thanks anyway.
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Hi Valentin,
Thanks again for your detailed feedback, I really appreciate it. You are absolutely right abt line 10 and instanceof operator code. The book "Java Rules" is not available in India for purchasing, but I'll see if I can procure it from a library or sth. The line 10 should probably change to -
if((obj == null) || (obj.getClass() != this.getClass())) to take care of null as well. Right?
The rest of the article will need changes accordingly, I'll make these changes.
Guys, let me also know if there are any mistakes, ambiguity in the test questions.
rgds,
- Manish
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Manish:
Who is the author of this article?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
The line 10 should probably change to -
if((obj == null) || (obj.getClass() != this.getClass())) to take care of null as well. Right?

I would write it this way

That's only a matter of taste, you can also write it your way
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Sun LiWei:
I can't visit www.geocities.com,coz I come from China,And I don't know a proxy server too.Could someone do me a favor,email the article and the mock test ??Thanks anyway.

Share or send your email addres. Wait for 2/3 days so that I can make the required changes and u get the correct and updated info.
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Barkat Mardhani:
guess what...I worked this time. I will give my feedback after I am done reading....

Great!!!
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Valentin Crettaz:
The line 10 should probably change to -
if((obj == null) || (obj.getClass() != this.getClass())) to take care of null as well. Right?

I would write it this way

That's only a matter of taste, you can also write it your way

Yeah,
I would also prefer this style, for better readability. I meant that only the conditon

will not take care of null value.
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I meant that only the conditon will not take care of null value.
That's correct, I was just being picky
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Valentin,
The info you shared from Java Rules indeed is very interesting. I do not have that book, but I have been thinking abt it. It only makes more sense everytime I think abt it.
Joshua Bloch mentions this in his book "Effectve Java" chapter 3, on which this article is based -
"....It turns out that this is a fundamental problem of equivalence relations in object-oriented languages. There is simply no way to extend an instantiable class and add an aspect while preserving the equals contract."

But then, the base class should indeed return false if an object of subclass is passed to its equals() method. So Java Rules makes sense.
Interestingly, few built-in non-final (instanceof check makes sense in case of final classes) Java classes use the instanceof check, java.util.Date is one of them. It has three known subclasses java.sql.Date, java.sql.Time and java.sql.Timestamp. Out of which, first two classes inherit their equals method from java.util.Date, so symmetry is not violated there and java.sql.Timestamp which has its own implementation of the equals() method mentions in its API documentation abt vilation of Symmetry principle. However, since java.util.Date is a non-final class I believe it is its duty to ensure that its equals method returns false if a subclass obejct is passed to it. It *does not*; because java.util.Date also uses instanceof check in its equals method implementation. Here is it -

And here is a small code that I wrote which shows violation of symmetry principle in equals method of java.util.Date (with JDK 1.4 on win 2k)

So am I correct if I say that the equals method of java.util.Date has a bug? Is it a known bug?
- Manish
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
java.sql.Timestamp which has its own implementation of the equals() method mentions in its API documentation abt vilation of Symmetry principle.
I think this has to do with bug ID 4096949
So am I correct if I say that the equals method of java.util.Date has a bug? Is it a known bug?
I would say yes, but I didn't find any trace of it in the Sun's bug database :roll:
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Originally posted by Valentin Crettaz:
java.sql.Timestamp which has its own implementation of the equals() method mentions in its API documentation abt vilation of Symmetry principle.
I think this has to do with bug ID 4096949
So am I correct if I say that the equals method of java.util.Date has a bug? Is it a known bug?
I would say yes, but I didn't find any trace of it in the Sun's bug database :roll:

I think it is somewhat acceptable in the case of java.sql.Timestamp, as it is documented in the API, so one is forewarned abt the violation of symmetry and consequently abt the problems it would create if java.sql.Timestamp and java.util.Date are used together in the same collection. But, java.util.Date bug should be reported, provided it indeed is a bug. Apperantly, it is. :roll:
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

I did manage to procure a copy of "Java Rules" from the local library. I got lucky!!!
After reading through the details Valentin mentioned, I have made all the necessary changes in the article. I have also made the other changes that he pointed out. The article now contains more information about the hashCode method as well. Also, some of the mock test questions have been changed and reworded accordingly. Do read the article and let me know your feedback on both, the article and the test questions.
- Manish
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

The bug ID 4096949 deals with the incorrect signature of the method, and the overloading of method that takes place due to this; not the volation of symmetry bug.
I have also read abt it in the book Practical Java by Peter Haggar Praxis 11 to 16, he also recommends use of getClass() over instanceof, but he points out that the instanceof check might be required in some non-final classes when the subclass only alters behaviour of the object, in that case, it becomes subclass's responsibility to treat equals correctly. The condition imposed by getClass() approach may seem too strict at times. The conflict between instanceof and getClasss() is also discueed in bug ID - 4049974.
- Manish
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Thx Manish for your article
I was reading some old posts and saw this article about the same subject. I thought it might be helpful.
http://developer.java.sun.com/developer/Books/effectivejava/Chapter3.pdf


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2581

Shishio,
Glad to know that you found my article useful. The link you have given is very much included in the resources list at the end of this article.
- Manish
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals and Hash Code