Dec 16, 2008

equals() and ==

== operator evaluates to true, only when both references refer to the same object
e.g reference a------------> Object1
reference b------------> Object1
then a == b returns true.

  1. String class and Wrapper classes has override equals() method , so that two different objects could be compared to see if their contents are meaningfully equal.
  2. If classes equals() method is not overridden then it cannot be used as key in a Hashtable.
  3. equals() method in Object Class use only the == operator for comparison.
  4. If two objects are considered equal by using the equals() method then they should have identical hashcode values. So it is advisable to override hashCode() when equals() is overridden.

No comments:

Post a Comment