Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

advantages and disadvantages of the method equal()  RSS feed

 
caleb danboyi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
 
Jason Bullers
Ranch Hand
Posts: 115
11
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
i am talking in terms of of using it in my own code
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • 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.
 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It can still be found here: Chapter 3, Methods Common to All Objects - Joshua Bloch
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • 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.
 
Ryan Gabriel
Greenhorn
Posts: 20
Android Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!