Let A be a class with some members as x, y, z:
Class A { int x; int y; String z; ... }
A is an Object so it inherits the "Equals" functions defined in Object. What is the default behavior of this function? Does it check for the equality of members or does it check for reference equality?
equals() method compares the object references or the memory location where the objects are stored in the heap. Thus by default the . equals() method checks the object by using the “==” operator.
The equals method checks by default whether the object given as a parameter has the same reference as the object it is being compared to. In other words, the default behaviour checks whether the two objects are the same. If the reference is the same, the method returns true , and false otherwise.
equals(Object otherObject) – verifies the equality of two objects. Its default implementation simply checks the object references of two objects to verify their equality. By default, two objects are equal if and only if they are refer to the same memory location.
The default implementation calls Object. Equals(Object) on each field of the current instance and obj and returns true if all fields are equal. Particularly if your value type contains fields that are reference types, you should override the Equals(Object) method.
The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.
http://msdn.microsoft.com/en-us/library/bsc2ak47.aspx
it checks for reference unless you override equals
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With