previous
|
start
|
next
Creating Hash Codes for your Classes
Your
hashCode
method must be compatible with the
equals
method
if
x.equals(y)
then
x.hashCode() == y.hashCode()
You get into trouble if your class defines an
equals
method but not a
hashCode
method
If we forget to define
hashCode
method for
Coin
it inherits the method from
Object
superclass
That method computes a hash code from the memory location of the object
Effect: any two objects are very likely to have a different hash code
Coin coin1 = new Coin(0.25, "quarter"); Coin coin2 = new Coin(0.25, "quarter");
In general, define either both
hashCode
and
equals
methods or neither
previous
|
start
|
next