File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help understanding .equals.

 
Rajesh Khan
Ranch Hand
Posts: 230
  • 0
  • Mark post as helpful
  • send pies
  • 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




 
Alok Aparanji
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so why does the .equal check the contents in this case ??


ouput : same contents
 
Alok Aparanji
Greenhorn
Posts: 4
  • 2
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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 ==.
 
Rob Spoor
Sheriff
Pie
Posts: 20369
44
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 1
  • Mark post as helpful
  • send pies
  • 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.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic