0

I saw a code segment regarding defining equals, from another question thread in this forum. But my question is what does the following code does, and why it is needed? Thanks.

   if (obj == this)
   {
       return true;
   }

The original code is shown as follows.http://stackoverflow.com/questions/8338326/what-does-equalsobject-obj-do#

public boolean equals(Object obj)
{
if (obj == this)
{
    return true;
}
if (obj == null)
{
    return false;
}
if (obj instanceof Contact)
{
    Contact other = (Contact)obj;
    return other.getFirstName().equals(getFirstName()) &&
            other.getLastName().equals(getLastName()) &&
            other.getHomePhone().equals(getHomePhone()) &&
            other.getCellPhone().equals(getCellPhone());

}
else
{
    return false;
}

}

user288609
  • 12,465
  • 26
  • 85
  • 127

5 Answers5

2

Implementing the equals method of a Java class is an often discussed topic. In general you want to ensure, that the method returns true, if:

  • the object you compare this with is the same object regarding the pointer (object == this) or
  • the object you compare this with is the same regarding your domain

So regarding your question, you ask for the former case, in which Java just checks if the object passed and this are pointing to the same address in memory (maybe this link helps you http://www.javaworld.com/article/2072762/java-app-dev/object-equality.html).

A short note: When you implement the equals method you normally follow this pattern:

  1. check if you have the same object (regarding pointers), i.e.

    if (object == this) return true;
    
  2. you make sure that you don't have any null instance (to avoid further NullPointerException and if it's null it can never be equal, i.e.,

    else if (object == null) return false;
    
  3. you check if the object is equal regarding whatever equal means in your domain (e.g., a dao might be assumed to be equal if the id is equal); prior to doing the domain specific validation, you normally always make sure that you have an object of the correct instance, i.e.,

    else if (this.getClass().equals(obj.getClass()) { ... }
    

    or

    else if (this.getClass().isInstance(obj.getClass()) { ... }
    
  4. in all other cases you want to return false, i.e.,

    else return false;
    

NOTE: When implementing the equals method it is often useful to use the Objects.equals(...) method to compare the different attributes of the instances.

EXAMPLE:

@Override
public boolean equals(final Object obj) {
    if (obj == this) {
        // we have the same object regarding the pointers (e.g., this.equals(this)), or have a look at 'Manjunath M' post for a different example
        return true;
    } else if (obj == null) {
        // we compare with null (e.g., this.equals(null))
        return false;
    } else if (Contact.class.isInstance(obj.getClass())) {
        /*
         * If you want to be more strict you could also use:
         * getClass().equals(obj.getClass())
         */
        Contact other = Contact.class.cast(obj);
        return Objects.equals(other.getFirstName(), getFirstName()) &&
                Objects.equals(other.getLastName(), getLastName()) &&
                Objects.equals(other.getHomePhone(), getHomePhone()) &&
                Objects.equals(other.getCellPhone(), getCellPhone());
    } else {
        return false;
    }
}
Philipp
  • 1,289
  • 1
  • 16
  • 37
1

'==' in Java is used for comparing the references of objects. If the object is compared with own self then equals function must return true. That's why this condition is added in equals method.

for primitive datatypes like int,char '==' this compares values and for objects it compares there references. Some literature says that working of '==' is really weired as comparing references is not really important.

Prashant
  • 138
  • 8
0

this refer's to the same object inside which the if statement is. By comparing the obj== this it checks if the object passed is the object itself inside which the equals function is called.

you can check this question too: What is the meaning of "this" in Java?

Community
  • 1
  • 1
Denis
  • 1,219
  • 1
  • 10
  • 15
0

If you are not overriding the equals method in your respective class it executes the implementation that already written in the equals method in Object class(The super most class for all the classes) there they are checking the equality like this

obj == obj1

the code mentioned above is actually checks the equality based on the memory location address that those two objects where they are created, thus above line checks whether two objects that we are trying to check equality have same reference or not. If both have same reference we don't have to do any more circus for checking equality. like this

Car car = new Car();
Car car1 = car;

car.equals(car1);

As you can clearly see that car and car1 have same reference means both are same object having same memory address. Thus don't have to check for the equality by checking attributes.it just avoids execution extra code.

Manjunath M
  • 588
  • 1
  • 6
  • 30
0

this refers to the class's object under which the equals function is being executed. If obj is the other object that needs to be compared for equality and obj and this refer to the same instance then they are true, right?

For example

MyClass myObject = new MyClass();
myObject.equals(obj);

In this case both this and obj in the equals method would refer to the same instance myObject. In such cases we don't need to check other properties of these objects for equality.

TL;DR - Checking for such case helps in optimization as this avoids extra work needed to check the two objects for equality.

Harshil Sharma
  • 2,016
  • 1
  • 29
  • 54