Effective Java - Implementing equals()
Functions such as equals(), compareTo(), hashCode(), clone(), and finalize()
belong to the basic infrastructure that practically every class type must
provide. Yet it is amazing how little agreement there is in the Java
community as how to implement them correctly. Java gurus, such as recognized
authors and experienced Java developers, are in serious disagreement regarding
correct implementation of these methods.
In this tutorial we look into the semantics and implementation of the
equals() method. For illustration of the related difficulties and misconceptions
we study a number of implementations of equals() that have been published
by recognized authors in their books or by experienced Java developers
in commonly available Java source code. We will find that some of the implementations
are incorrect, others are correct, and some are debatable and fragile.
We aim to explain the different underlying strategies and to evaluate their
up- and downsides. Eventually we condense our conclusions to a set of guidelines
that help Java programmers to avoid common mistakes and to provide correct
and robust implementations of equals(). |
|
PREREQUISITES
|
|
Level:
|
intermediate |
Duration:
|
90 min |
Prerequisites:
|
Attendants should be familiar with the basic concepts of
Java. |
Presented at:
|
ACCU Conference
2002
, Oxford, UK, April 2002
OOP Europe 2002
, Amsterdam,
Netherlands, June 2002 |
If you are interested to hear more about this
and related topics you might want to check out the following seminar or
skim through some further reading:
|
|
|
Seminars
|
Papers
|
Secrets
of equals()
pulished in Java Solutions - A
Supplement of C/C++ Users Journal
(April 2002)
|
|
Slides
|
|