It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Reference variables comparison using equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Reference variables comparison using equals method" Watch "Reference variables comparison using equals method" New topic

Reference variables comparison using equals method

vvus bharadwaj
Ranch Hand

Joined: Apr 28, 2012
Posts: 38
What happens when we compare reference variables using equals method?(equals method compares what?)
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

look this post Equal()

Tell the difficulties that i am difficult.
gurpeet singh
Ranch Hand

Joined: Apr 04, 2012
Posts: 924

if the objects you are comparing have overridden equals method( from java.lang.Object class) then it depends upon your implementation. as to how you implements equals method for establishing the equality of two objects. say you have a Dog class with instance field as String breed, with getter for breed field. you can writer your equals method as follows : here we are establishing how 2 dog objects can be equal. we used breed instance field. you can use your own implementation. inside equals method , i call equals method on Sting. Since string class has already overridden equals method it can compare 2 strings and can return true, or false depending upon the literal string contained in the two string objects. also , if you don't override equals method then inherited equals method from the Object will run, which uses == operator for comparisons, which further means two references are only equal if they refer to same object.

I agree. Here's the link:
subject: Reference variables comparison using equals method
jQuery in Action, 3rd edition