I am struggling to understand this Koan:
@Koan
public void equalsMethodCanBeChangedBySubclassesToTestsIfTwoObjectsAreEqual() {
Object object = new Integer(1);
assertEquals(object.equals(object), true);
assertEquals(object.equals(new Integer(1)), __);
// Note: This means that for the class 'Object' there is no difference between 'equal' and 'same'
// but for the class 'Integer' there is difference - see below
}
As far as I understand, because object
is an instance of the Object
class, the .equals()
method has not been overwritten, and therefore checks for object equality.
If new Integer(1)
creates a new instance, then it should be a separate object to object
. Following my train of thought, the correct answer should be false
, but only true
makes this pass. Where is the flaw in my logic?
Edit: I understand that integers between -128 and 127 are cached. If my understanding of the object
object is correct (as stated above), then this is irrelevant.