Finalizes the object during garbage collection. This method is discussed in
detail in "Finalization" on page 449.
public StringtoString()
Returns a string representation of the object. The toString method is
implicitly invoked whenever an object reference is used within a string
concatenation expression as an operand of the + operator. The Object
version of toString constructs a string containing the class name of the
object, an @ character, and a hexadecimal representation of the instance's
hash code.
Both the hashCode and equals methods should be overridden if you want to provide a notion of equality
different from the default implementation provided in the Object class. The default is that any two different
objects are not equal and their hash codes are usually distinct.
If your class has a notion of equality in which two different objects can be equal, those two objects must
return the same value from hashCode. The mechanism used by hashed collections relies on equals
returning TRue when it finds a key of the same value in the table. For example, the String class overrides
equals to return TRue if the two String objects have the same contents. It also overrides hashCode to
return a hash based on the contents of the String so that two strings with the same contents have the same
hashCode.
The term identity is used for reference equality: If two references are identical, then == between the two will
be true. The term equivalence describes value equalityobjects that may or may not be identical, but for
which equals will return true. So one can say that the default implementation of equals is that
equivalence is the same as identity. A class that defines a broader notion of equality can have objects that are
not identical be equivalent by overriding equals to return TRue based on the states of the objects rather
than their identities.
Some hashtables, such as java.util.IdentityHashMap, are concerned with identity of objects, not
equivalence. If you need to write such a hashtable, you want hash codes corresponding to the identity of
objects, not their states. The method System.identityHashCode returns the same value that the
Object class's implementation of hashCode would return for an object if it were not overridden. If you
simply use hashCode on the objects you are storing, you might get a hash code based on equivalence, not on
identity, which could be far less efficient.
Exercise 3.7: Override equals and hashCode for ColorAttr.
3.9. Cloning Objects
The Object.clone method helps you write clone methods for your own classes. A clone method returns a
new object whose initial state is a copy of the current state of the object on which clone was invoked.
Subsequent changes to the new clone object should not affect the state of the original object.
3.9.1. Strategies for Cloning
There are three important factors in writing a clone method:
The empty Cloneable interface, which you must implement to provide a clone method that can
be used to clone an object.[3]