• 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

help understanding .equals.

 
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read that == is tells us whether the two objects point to the same address and .equal checks the contents of the address rite ??
so why isnt the .equals working in the following code . In other words how would i know when i need to overload the .equals method ?? Any suggestions please..



output:: Different




 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh Khan wrote:I read that == is tells us whether the two objects point to the same address and .equal checks the contents of the address rite ??
so why isnt the .equals working in the following code . In other words how would i know when i need to overload the .equals method ?? Any suggestions please..



output:: Different





Rajesh, class c needs to override equals. Else its same as == (by default). All class that you create which uses 'equals' need to override equals. And please note, its override not overload.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
equals() only compares the contents of the object if the equals() method has been overridden to do that. If it isn't overridden then you're using the version of equals() that's inherited from Object - and that just uses == to check if they're the same object.
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so why does the .equal check the contents in this case ??


ouput : same contents
 
Alok Aparanji
Greenhorn
Posts: 4
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh Khan wrote:so why does the .equal check the contents in this case ??


ouput : same contents


Obviously, Integer, String etc override equals()
 
Rajesh Khan
Ranch Hand
Posts: 230
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh okay so in short for every custom object that i create ill need to override the .equals method since the .equals by default resembles the == is my understanding correct now.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh Khan wrote:I read that == is tells us whether the two objects point to the same address



Not quite. The == operator tells us whether two reference values are equal. That is, whether two references point to the same object, or both are null. Objects don't point to anything. References point to objects.

and .equal checks the contents of the address rite ??


The equals() method does whatever the author of the class wants it to, but the correct behavior is to compare fields ("contents") for equality, so that if two distinct objects have the same values in the appropriate fields (as per that class's semantics), then they are considered equal.

If you don't override equals(), you inherit it from Object (or from whichever ancestor did override it.) Object's equals() just does ==.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rajesh Khan wrote:Oh okay so in short for every custom object that i create ill need to override the .equals method



If you want to be able to compare those objects' states (contents) using equals(), yes. This is necessary or desirable for all classes though.

since the .equals by default resembles the == is my understanding correct now.

Yes, Object's equals() method just does ==.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't forget to override hashCode when you override equals, or your objects will fail miserably in HashSet and HashMap instances.
 
Marshal
Posts: 79278
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Three resources for writing equals methods:
  • 1: Effective Java™ by Joshua Bloch
  • 2: Angelika Langer’s website, which has several pages about equals and hash codes.
  • 3: An article by Odersky, Spoon, and Venners, which you can find from this FAQ.
  • Thise are all worth reading.

    Beware: Lots of people will tell you it is easy to write equals() methods; this is not true.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic