Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

advantages and disadvantages of the method equal()

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The class Object defines an equal() method to compare objects.
so please you all should help me
Specify advantages and disadvantages of using this method and
suggest an alternative for equality
 
Ranch Hand
Posts: 115
11
IntelliJ IDE Clojure Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure what you're looking for with this question. Are you talking in terms of design decisions made by the Java guys when they created equals(), or in terms of using equals() in your own code? Any "alternate" checks for equality aren't supported by the general contract of object equality, so while it might seem helpful to write your own, be very careful with it (I can't think of very many reasons to provide an overloaded equals()). Be aware that your code would be the only code using your alternate implementation; it will not be used automatically by the collections API, for example.
 
caleb danboyi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am talking in terms of of using it in my own code
 
Saloon Keeper
Posts: 14327
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The advantage is that you can check two objects for equality, obviously.

If you need to see if two objects are the same, you use it. If you don't need it, don't use it. It's not a matter of pros and cons.
 
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Take note of what Jason Bullers said; the equals() method can be very difficult to override correctly. There are there well‑known resources, which I have listed in this link. I am afraid the downloadable sample chapter I mentioned appears no longer to be available. If you find it please tell us.
 
Sheriff
Posts: 3919
542
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can still be found here: Chapter 3, Methods Common to All Objects - Joshua Bloch
 
Campbell Ritchie
Marshal
Posts: 76486
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done finding that link

It used to be used as a sample chapter for people considering buying the book, so I think there is no copyright problem if you read it free of charge. At lest I think it is all right.
 
Greenhorn
Posts: 20
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome buddy
My opinion is almost same as Stephan van Hulst said -

If you need to see if two objects are the same, you use it. If you don't need it, don't use it. It's not a matter of pros and cons.

Have a look on Marcus Biel's lecture on equals, It helped me a lot on -
hashcode and equals
 
What's a year in metric? Do you know this metric stuff tiny ad?
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic