Java Reference
In-Depth Information
// Use price
long priceBits = Double.doubleToLongBits(price);
code = (int)(priceBits ^ (priceBits >>>32));
hash = hash * 59 + code;
return hash;
}
}
The class has five instance variables:
title
,
author
,
pageCount
,
hardcover
, and
price
. The implementation uses
all five instance variables to compute the hash code for a
Book
object. You must also implement the
equals()
method
for the
Book
class, which must use all the five instance variables to check if two
Book
objects are equal. You need to
make sure that the
equals()
method and the
hashCode()
method use the same set of instance variables in their
logic. Suppose you add one more instance variable to the
Book
class. Let's call it
ISBN
. Because
ISBN
identifies a book
uniquely, you might use only the
ISBN
instance variable to compute its hash code and to compare for equality with
another
Book
object. In this case, it will be sufficient to use only one instance variable to compute the hash code and
check for equality.
There are some misconceptions about the hash code of an object in Java. Developers think that the hash code
uniquely identifies an object and it must be a positive integer. However, they are not true. The hash code does not
identify an object uniquely. Two distinct objects may have the same hash codes. A hash code does not have to be
only a positive number. It could be any integer value, positive as or negative. There is also confusion about the usage
of hash codes. They are used solely for the purpose of efficient retrieval of data from a hash-based collection. If your
objects are not used as keys in hash based collections and you do not override the
equals()
method in your class,
you do not need to worry about reimplementing the
hashCode()
method in your class at all. Most likely, it will be
overriding the
equals()
method that will prompt you to override the
hashCode()
method for your class. If you do not
override and provide correct implementation of
hashCode()
and
equals()
methods in your class at the same time, the
objects of your class would not behave properly in hash-based collections. The Java compiler or the Java runtime will
never give you any warnings or errors about the incorrect implementations of these two methods in your class.
Comparing Objects for Equality
Every object in the universe is different from all other objects, and every object in a Java program is different from all
other objects. All objects have a unique identity. The memory address at which an object is allocated can be treated as
its identity, which will make it always unique. Two objects are the same if they have the same identity (or reference in
Java terminology). Consider the following snippet of code:
Object obj1;
Object obj2;
/* Do something... */
if (obj1 == obj2) {
/* obj1 and obj2 are the same object based on identity */
}
else {
/* obj1 and obj2 are different objects based on identity */
}
The above code uses identity comparison to test for equality of
obj1
and
obj2
. It compares the references of two
objects to test whether they are equal.