----- Original Message -----
From: "David Barker" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, September 13, 2000 2:18 AM
Subject: Re: Writing a uniform hashCode method


> ----- Original Message -----
> From: "Jonathan K. Weedon" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Tuesday, September 12, 2000 17:18
> Subject: Writing a uniform hashCode method
>
>
> > Consider the seemingly trivial example of a primary key
> > containing two integers:
> >
> >         public class MyKey implements java.io.Serializable {
> >
> >           public int a;
> >           public int b;
> >
> >           public boolean equals(Object object) {
> >             if(object == null || !(object instanceof MyKey)) {
> >               return false;
> >             }
> >             MyKey that = (MyKey) object;
> >             return this.a == that.a && this.b == that.b;
> >           }
> >
> >           public int hashCode() {
> >             return a ^ b;
> >           }
> >
> >         }
> >
> > This was not very hard, was it?  Unfortunately, it was
> > wrong.
> >
> > (You might now stop reading, and try to figure out the
> > bug in the above code.  I will explain it in what follows.
> > Kudos if you do see the bug.  Hint: the bug is not in the
> > equals method, but in the hashCode method.)
> >
>
> Well actually the implementation of the equals method could be improved
> as it can erroneously return true when comparing instances of MyKey to
> instances of sub classes of MyKey
> see http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object_p.html
> for an interesting discussion on this issue
>
> Of course this just lends credence to the fact that it is hard to
correctly
> implement equals and hashCode
>
> ...David
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to