Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equal(Object o) doubt

 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Arad Chear
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thaaanks , marc for inform the mistake
 
m ali
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic