Java Reference
In-Depth Information
addition steps “spread out” the range of hash codes and dramatically reduce the
likelihood that two unequal
Circle
objects have the same code.
Efective Java
by
Joshua Bloch (Addison Wesley) includes a helpful recipe for constructing efficient
hashCode()
methods like this one.
Comparable::compareTo()
Example 5-1
includes a
compareTo()
method. This method is defined by the
java.lang.Comparable
interface rather than by
Object
, but it is such a common
method to implement that we include it in this section. The purpose of
Comparable
and its
compareTo()
method is to allow instances of a class to be compared to each
other in the way that the
<
,
<=
,
>
, and
>=
operators compare numbers. If a class
implements
Comparable
, we can say that one instance is less than, greater than, or
equal to another instance. This also means that instances of a
Comparable
class can
be sorted.
Because
compareTo()
is not declared by the
Object
class, it is up to each individual
class to determine whether and how its instances should be ordered and to include a
compareTo()
method that implements that ordering. The ordering defined by
Example 5-1
compares
Circle
objects as if they were words on a page. Circles are
first ordered from top to bottom: circles with larger
y
coordinates are less than cir‐
cles with smaller
y
coordinates. If two circles have the same
y
coordinate, they are
ordered from left to right. A circle with a smaller
x
coordinate is less than a circle
with a larger
x
coordinate. Finally, if two circles have the same
x
and
y
coordinates,
they are compared by radius. The circle with the smaller radius is smaller. Notice
that under this ordering, two circles are equal only if all three of their fields are
equal. This means that the ordering defined by
compareTo()
is consistent with the
equality defined by
equals()
. This is very desirable (but not strictly required).
The
compareTo()
method returns an
int
value that requires further explanation.
compareTo()
should return a negative number if the
this
object is less than the
object passed to it. It should return 0 if the two objects are equal. And
compareTo()
should return a positive number if
this
is greater than the method argument.
clone()
Object
defines a method named
clone()
whose purpose is to return an object with
fields set identically to those of the current object. This is an unusual method for
two reasons. First, it works only if the class implements the
java.lang.Cloneable
interface.
Cloneable
does not define any methods (it is a marker interface) so
implementing it is simply a matter of listing it in the
implements
clause of the class
signature. The other unusual feature of
clone()
is that it is declared
protected
.
Therefore, if you want your object to be cloneable by other classes, you must imple‐
ment
Cloneable
and override the
clone()
method, making it
public
.
The
Circle
class of
Example 5-1
does not implement
Cloneable
; instead it provides
a
copy constructor
for making copies of
Circle
objects: