• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equal(Object o) doubt

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thaaanks , marc for inform the mistake
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic