Win a copy of Java Database Connections & Transactions (e-book only) this week in the JDBC 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
  • Devaka Cooray
  • Knute Snortum
  • Paul Clapham
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Frits Walraven
Bartenders:
  • Ganesh Patekar
  • Tim Holloway
  • salvin francis

equals & hashcode doubt  RSS feed

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Output is true,false. That is the expected output.

What is the effect of return false in equals method & hash code return 0? I have seen there is no different in execution of code. Can anyone please explain?
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Remember that you have overriden the equals and the hashcode methods for the class containing the main method. However, you did not really override the hashcode and the equals method for the String class. Just as side note, you cannot override the equals and the hashcode methods for the String class because it is marked final.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the code you provided, the equals and hashCode method are not invoked as the Set you constructed is composed using String objects. Hence, only the String object's equals, hashCode methods would be invoked.

To illustrate the effect of your local equals and hashCode, you may create instances of the class that holds this code and then replace Strings everywhere in your code with this new class. You may optionally add print statements in equals and hashCode to verify that these methods are indeed invoked.
 
I love a woman who dresses in stainless steel ... and carries tiny ads:
how do I do my own kindle-like thing - without amazon
https://coderanch.com/t/711421/engineering/kindle-amazon
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!