aspose file tools*
The moose likes Java in General and the fly likes help understanding .equals. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help understanding .equals. " Watch "help understanding .equals. " New topic
Author

help understanding .equals.

Rajesh Khan
Ranch Hand

Joined: Oct 16, 2011
Posts: 230
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

Joined: Jan 08, 2012
Posts: 4
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

Joined: Apr 06, 2010
Posts: 4464
    
    8

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

Joined: Oct 16, 2011
Posts: 230
so why does the .equal check the contents in this case ??


ouput : same contents
Alok Aparanji
Greenhorn

Joined: Jan 08, 2012
Posts: 4
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

Joined: Oct 16, 2011
Posts: 230
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: Oct 27, 2005
Posts: 19760
    
  20

Don't forget to override hashCode when you override equals, or your objects will fail miserably in HashSet and HashMap instances.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
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.
     
    wood burning stoves
     
    subject: help understanding .equals.