This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

equal(Object o) doubt

 
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • 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
 
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.
 
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
 
Crusading Chameleon likes the size of this ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic