SCJP Certification

Sun Certified Java Programmer Certification exam essentials

August 27th, 2009

What will happen if a program does not override the equals() method?

Java Facts, by Daisy Williams.

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:

  1. Objects will not be used as a hashing key.
  2. 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.
  • Share

Back Top

Responses to “What will happen if a program does not override the equals() method?”

Comments (0) Trackbacks (0) Leave a comment Trackback url
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Your email address will not be published. Required fields are marked *

*