The equals( ) method compares two objects for equality and returns 'true' if they are equal and 'false' otherwise. The method takes any object as an argument. When objects of a particular class must be compared for equality, the class should override the equals method to compare the contents of the two objects.
The method's use should meet these requirements:
1.) It should return 'false' if the argument is null.
2.)It should return 'true' if an object is compared to itself, as in object1.equals(objects1).
3.)It should return 'true' only if both object1.equals(object2) and objects2.equals(object1) would be true.
4.)For three objects, if object1.equals(object2) returns 'true' and object2.equals(object3) returns 'true' then object1.equals(object3) should also return 'true'.
Here is an implementation of equals for a data-centric class. It demonstrates how different types of fields are treated:
object fields, including collections : use equals
type-safe enumerations : use either equals or == (they amount to the same thing, in this case)
possibly-null object fields : use both == and equals
array fields : use Arrays.equals
primitive fields other than float or double : use ==
float : convert to int using Float.floatToIntBits, then use ==
double : convert to long using Double.doubleToLongBits, then use ==