equals() method checks whether or not the instances refer to the same object. equals() method. It is defined in the java.lang.Object class. It is used to test the equality of objects.The following will occur if a program does not override the equals() method:
- Objects will not be used as a hashing key.
- Objects will not be considered equal.
Key rules:
- The equals() method in the class Object uses only the == operator and the instanceof operator for comparisons. Unless a user overloads the equals() method, it will not compare the different objects because they are considered equal if their references refer to the same object.
- The equals() method must be overridden, if the objects of a program are going to be used as keys for a hashtable or for retrieving or searching an object. It is quite necessary to override the equals() method, so that all instances can be considered the same.
Responses to “What will happen if a program does not override the equals() method?”