Reference variables comparison using equals method
posted 4 years ago
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.