| Author |
equal(Object o) doubt
|
Arad Chear
Ranch Hand
Joined: Jan 05, 2007
Posts: 98
|
|
answer 3,4 why 4 , since rate has not mention in hashCode , so there is possible two find two Object equal but different hashCode ! from K&B
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Originally posted by Eisa Ayed: ...there is possible two find two Object equal but different hashCode ! ...
I agree. The 4th option should not be correct. Here's an example...
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Arad Chear
Ranch Hand
Joined: Jan 05, 2007
Posts: 98
|
|
|
Thaaanks , marc for inform the mistake
|
 |
m ali
Ranch Hand
Joined: Apr 12, 2007
Posts: 49
|
|
Hi ranchers, In the above post, Dont we need to check all the members variables of the class are equal or not along with the object's hashcode to ensure that the two objects of 'SortOf' class are equal?? Please clarify Thanks in Advance.
|
 |
Chandra Bhatt
Ranch Hand
Joined: Feb 28, 2007
Posts: 1707
|
|
Hi ranchers, In the above post, Dont we need to check all the members variables of the class are equal or not along with the object's hashcode to ensure that the two objects of 'SortOf' class are equal?? Please clarify Thanks in Advance.
It is OK, if you use all the fields of the class in hashCode() computation and in equals() method. But it is recommended that your equals() does work best using fewer fields. Suppose a person has Social Security Number that is unique for that Person. If you are using that SSN to compare two Person objects, your equals method gives efficient result. No more matching is required as well there. Comparing two Person objects on behalf of their age is inefficient. You can use more than one field, even all field to compute the equals() and it may give you more correct result. But we should try to pick the fields that we think better for use in equals(). And there should be good contract match between equals() and hashCode(). Regards, cmbhatt
|
cmbhatt
|
 |
 |
|
|
subject: equal(Object o) doubt
|
|
|