• 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

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: 15510
363
  • 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: 79179
377
  • 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.
 
Marshal
Posts: 4501
572
VSCode 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: 79179
377
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic