Object class equals() and hashcode() methods...
3 posters
Page 1 of 1
Object class equals() and hashcode() methods...
Hi guys,
Lets discuss a bit about the equals() and hashcode() methods in the Object class of java. Why are these provided and how are they related?
What is the default implementation of the equals() method in the Object class and how do we override it in subclasses?
HINT: 1) When do we say that two objects are equal?
2) How do we compare two string objects?
Let the discussions roll.
Regards,
Lester
Lets discuss a bit about the equals() and hashcode() methods in the Object class of java. Why are these provided and how are they related?
What is the default implementation of the equals() method in the Object class and how do we override it in subclasses?
HINT: 1) When do we say that two objects are equal?
2) How do we compare two string objects?
Let the discussions roll.
Regards,
Lester
lester.noronha- VIP Member
- Number of posts : 54
Registration date : 2008-04-29
Re: Object class equals() and hashcode() methods...
The Java super class java.lang.Object has two very important methods defined in it. They are -
public boolean equals(Object obj)
public int hashCode()
public boolean equals(Object obj)
This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This particular comparison is also known as "shallow comparison". However, the classes providing their own implementations of the equals method are supposed to perform a "deep comparison"; by actually comparing the relevant data members. Since Object class has no data members that define its state, it simply performs shallow comparison.
It is reflexive: for any reference value x, x.equals(x) should return true.
It is symmetric: for any reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
It is transitive: for any reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
It is consistent: for any reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the object is modified.
For any non-null reference value x, x.equals(null) should return false
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
public int hashCode()
This method returns the hash code value for the object on which this method is invoked. This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must be overridden in every class that overrides the equals method.
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
As compared to the general contract specified by the equals method, the contract specified by the hashCode method is relatively simple and easy to understand. It simply states two important requirements that must be met while implementing the hashCode method. The third point of the contract, in fact is the elaboration of the second point. Let's understand what this contract really means.
Hence
Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
public boolean equals(Object obj)
public int hashCode()
public boolean equals(Object obj)
This method checks if some other object passed to it as an argument is equal to the object on which this method is invoked. The default implementation of this method in Object class simply checks if two object references x and y refer to the same object. i.e. It checks if x == y. This particular comparison is also known as "shallow comparison". However, the classes providing their own implementations of the equals method are supposed to perform a "deep comparison"; by actually comparing the relevant data members. Since Object class has no data members that define its state, it simply performs shallow comparison.
For any non-null reference value x, x.equals(null) should return false
The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true).
Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equal objects must have equal hash codes.
public int hashCode()
This method returns the hash code value for the object on which this method is invoked. This method returns the hash code value as an integer and is supported for the benefit of hashing based collection classes such as Hashtable, HashMap, HashSet etc. This method must be overridden in every class that overrides the equals method.
Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.
- The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
- If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
- It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
As compared to the general contract specified by the equals method, the contract specified by the hashCode method is relatively simple and easy to understand. It simply states two important requirements that must be met while implementing the hashCode method. The third point of the contract, in fact is the elaboration of the second point. Let's understand what this contract really means.
- Consistency during same execution - Firstly, it states that the hash code returned by the hashCode method must be consistently the same for multiple invocations during the same execution of the application as long as the object is not modified to affect the equals method.
- Hash Code & Equals relationship - The second requirement of the contract is the hashCode counterpart of the requirement specified by the equals method. It simply emphasizes the same relationship - equal objects must produce the same hash code. However, the third point elaborates that unequal objects need not produce distinct hash codes.
Hence
Equal objects must produce the same hash code as long as they are equal, however unequal objects need not produce distinct hash codes.
ujjwal.khare- Developer
-
Number of posts : 15
Age : 42
Location : Bangalore
Registration date : 2008-05-08
Re: Object class equals() and hashcode() methods...
Hi guys,
Just a question here. Sometime back I ran across an interview question.
Can there be a scenario where the hashcode() needs to be overridden but not the equals() method on a java object?
I have not got an answer to this question yet.
Please could someone post the reply to this if they know of such a scenario?
Regards,
Lester.
Just a question here. Sometime back I ran across an interview question.
Can there be a scenario where the hashcode() needs to be overridden but not the equals() method on a java object?
I have not got an answer to this question yet.
Please could someone post the reply to this if they know of such a scenario?
Regards,
Lester.
lester.noronha- VIP Member
- Number of posts : 54
Registration date : 2008-04-29
Re: Object class equals() and hashcode() methods...
There may be various cases but twi which I think of are
1:- Serialization, which require only identity hashcode.
2:- Any map like structure which is write only. i.e. you only want efficient writes based on properly overridden hashCode() and dont require equals().
1:- Serialization, which require only identity hashcode.
2:- Any map like structure which is write only. i.e. you only want efficient writes based on properly overridden hashCode() and dont require equals().
ujjwal.khare- Developer
-
Number of posts : 15
Age : 42
Location : Bangalore
Registration date : 2008-05-08
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum