• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion with equals( ) and ==  RSS feed

 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I thought that == is a shallow equals, which just compares the object references of two objects. This operator returns true if two references have the same memory address, which therefore means point to the same object.

I also thought that .equals() is a deep equals that compares the instance variables of the two objects in question, only returning true if they match. The two objects being compared don't necesserily have to have the same memory address, they could be different objects with the same values.

However, in Janes notes linked to from Javaranch (http://www.janeg.ca/scjp/oper/equals.html) it says that equals() "returns true if and only if the two variables being compared hold a reference to the SAME object".

This doesn't seem to make sense to me, as this describes the funtion of the == equals, not the .equals().

Could someone shed some light on this please?

Thank you,
Arthur
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's the *default* implementation of equals() (the one defined in java.lang.Object). Other classes may - or may not - override the equals() method with different behavior.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Janes notes linked to from Javaranch it says that equals()
>> "returns true if and only if the two variables being compared
>> hold a reference to the SAME object".
>> This doesn't seem to make sense to me

It doesn't make sense because it's entirely incorrect. I assume it's just an oversight. Here's the deal. Java does not have operator overloading and that includes the == operator. So == always behaves the same and for objects that means it returns true if and only if both variables reference the SAME object.

That is NOT true for the equals() method which, like all methods, can and frequently is overridden. That is where a deep comparison could be implemented but it's not automatic. The class must provide the internal comparison and it relies on its constituent classes to do the same.
 
Arthur Blair
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for you reply, Steve.

So just to clarify. The default Object.equals() method and the == operator have the same functionality when comparing two objects. They compare whether the references point to the same object.

So, if I want to implement a "deep" comparison, i need to provide an implementation of the .equals() method for my class that compares each instance variable individually.

I think I've got it now. Let me know if I am off the mark.

Regards,
Arthur
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arthur Blair:
Thank you for you reply, Steve.

So just to clarify. The default Object.equals() method and the == operator have the same functionality when comparing two objects. They compare whether the references point to the same object.

So, if I want to implement a "deep" comparison, i need to provide an implementation of the .equals() method for my class that compares each instance variable individually.

I think I've got it now. Let me know if I am off the mark.

Regards,
Arthur


Yes, that is exactly right. The equals() method only provides a "deep comparison" if you override it to do so.

Layne
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!