Cloned objects are not the same as when comparing with the Object.equals () method?

6

I was doing some equality testing, and when comparing two objects, being a clone of the other, I noticed that equals returns false , even the objects being identical. The return should not be true since they are cloned objects? Why does this happen?

Object Class:

public class SomeObject  implements Cloneable{

    private int identifier;
    private String someDescription;

    public SomeObject() {

    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    public SomeObject(int identifier, String someDescription) {
        super();
        this.identifier = identifier;
        this.someDescription = someDescription;
    }

    public int getIdentifier() {
        return identifier;
    }

    public void setIdentifier(int identifier) {
        this.identifier = identifier;
    }

    public String getSomeDescription() {
        return someDescription;
    }

    public void setSomeDescription(String someDescription) {
        this.someDescription = someDescription;
    }

    @Override
    public String toString() {
        return "[" + this.identifier + " - " + this.someDescription + "]";
    }
}

The test I did was:

SomeObject obj1 = new SomeObject(1, "Lorem ipsum");
SomeObject obj2 = (SomeObject) obj1.clone();

System.out.println("Object 1: " + obj1);
System.out.println("Object 2: " + obj2);

System.out.println(obj1.equals(obj2));

Results in

Object 1: [1 - Lorem ipsum]
Object 2: [1 - Lorem ipsum]
São iguais? false

Test reproduced in ideone .

    
asked by anonymous 11.01.2017 / 00:12

1 answer

7
  

Cloned objects not equal to the Object.equals () method?

No, certainly not. At least for the default implementation of equals .

According to the Java documentation:

  

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

Translation (by Google Translate)

  The equals method for the Object class implements the most discriminating equivalence relation possible on objects, that is, for any non-null reference values x and y , this method returns true if and only if x and y refer to the same object ( x == y has the value true ).

That is, the default behavior of the equals method is to check whether the two objects point to the same reference. And when an object is cloned it is created a new object with the same values as the old object.

So assuming the equals method has not been overwritten, this is exactly the expected behavior.

SomeObject obj1 = new SomeObject(1, "Lorem ipsum"); 
SomeObject obj2 = (SomeObject) obj1.clone();

System.out.println(obj1 == obj2);      //false
System.out.println(obj1.equals(obj2)); //false
    
11.01.2017 / 00:21