Are cloned objects not equal when comparing to Object.equals()?

Asked

Viewed 242 times

6

I was doing some equality tests, and when comparing two objects, being a clone of the other, I noticed that the equals returns false, even though the objects are 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 took 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.

  • 1

    Great question I also had this question ;D

  • 1

    Your method clone() will never launch CloneNotSupportedException because the clone() of Object only if the spear !(this instanceof Cloneable). So you can put one try-catch around the super.clone(), capture the CloneNotSupportedException and ignore it or encapsulate it in a AssertionError. With that you get rid of having the throws CloneNotSupportedException in its method and avoids making the other methods have to capture this exception.

  • 1

    Another ingrate downvote, serious people are voting a lot for empathy and dislike instead of voting for the quality of the post, votes are looking like facebook.

1 answer

7


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

No, certainly not. At least for the standard 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 method equals for the class Object implements the most discriminating equivalence relation possible in objects, i.e., for any non-zero 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 standard behavior of the method equals 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 method equals 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
  • So it gives in the same clone the object as I did or simply do SomeObject obj2 = new SomeObject(1, "Lorem ipsum"); , case of the example presented?

  • Right. Right. That’s the intention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.