Annotations
Annotations
can be applied to fields, methods, classes, and packages to embed
metadata alongside code. Annotations alone have no effect on a codebase,
but they can be detected during compile-time or during runtime to provide
additional functionality. For example, the @Override
annotation can be used
on methods that override a superclass method, allowing the compiler to prevent
bugs due to invalid method signatures. The @Column(name) annotation can
be used to
map fields to database columns, allowing data access objects
to dynamically generate SQL queries at runtime.
The Object Superclass
Every class in Java is a descendent,
directly or indirectly, of the Object class.
The Object class provides several methods that can be optionally extended
in subclasses.
Clone
The clone() method was originally designed to return a copy of an object that
implements the Cloneable interface. A clone can either be a shallow copy,
which shares the same references
as the original object, or a deep copy, which
copies the values of the original object into new objects. Unfortunately,
overriding this method correctly is difficult and it provides no benefit over
a
copy constructor, which offers more flexibility and a cleaner contract.
Equals
The equals() method compares two objects for equality. The default
implementation relies on the identity operator (==) to determine whether two
objects point to the same address in memory. Subclasses are encouraged
to override this method to test whether two objects
contain the same information
rather than the same location in memory. Note that if you override the
equals()
method you must by contract override the hashCode() method
as well.