0

the following is the equals method from the String class:

public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String aString = (String)anObject;
        if (coder() == aString.coder()) {
            return isLatin1() ? StringLatin1.equals(value,aString.value)
                              : StringUTF16.equals(value, aString.value);
        }
    }
    return false;
}

What does the comparison: 'this == anObject' at the first if statement mean?

1 Answers1

1

It compares the memory addresses for both the object passed as parameter and the one you call equals from. If they are in the same memory address, they are obviously the same object.

Otherwise, it keeps checking for other ways to compare if they are actually equivalent objects.

HaroldH
  • 364
  • 1
  • 3
  • 9
  • Oh, so you can reference the object on which the method is applied with the word 'this'. That is a vary useful feature. Thanks for the clear and concise answer. – Seongha Yi Sep 07 '18 at 19:28