0

I have tried this implementation but i got false for the class x

x.clone().equals(x)

Class X :

public class X implements Cloneable{
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    protected Object clone()throws CloneNotSupportedException {
        return super.clone();       
    }

}

Main class :

public class ObjectCloneCopy {
  public static void main(String[] args) throws CloneNotSupportedException {
    X x = new X();
    System.out.println("x.clone().equals(x) - " + x.clone().equals(x));
  }
}

Is it mandatory to overload the hashcode() and equals() to get this True ?

Without overriding these methods how this statement gives true?

X x1 = x;
x1.equals(x)

Explain how that could be true, i have seen in this link

Rajavel D
  • 1,250
  • 12
  • 28
  • 5
    [Didn't you just ask this question 2 minutes ago?](http://stackoverflow.com/questions/25284297/how-x-clone-equalsx-is-true) Don't delete your questions and just repost them. – Jeroen Vannevel Aug 13 '14 at 11:20
  • @JeroenVannevel Yes i asked after i deleted, i thought i may asked wrong. after i felt i should clarified my doubts so again undeleted... – Rajavel D Aug 13 '14 at 11:23
  • Without override hashcode and equals why this statement gives true : X x1 = x; x1.equals(x) – Rajavel D Aug 13 '14 at 11:38
  • possible duplicate of [Java == vs equals() confusion](http://stackoverflow.com/questions/7520432/java-vs-equals-confusion) – Raedwald Aug 13 '14 at 11:55

5 Answers5

5

You need to override equals() and hashCode() method in your X class.

Else you can't get the correct result from x.clone().equals(x)

Ruchira Gayan Ranaweera
  • 32,406
  • 16
  • 66
  • 105
1

Object#clone returns independent of clonning object, so two independent object may not be equals.

As per documentation -

Object#clone - 

Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. . The general intent is that -

x.clone() != x // true
x.clone().getClass() == x.getClass() // true

and

x.clone().equals(x) // will be true, this is not an absolute requirement.

By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().

Subhrajyoti Majumder
  • 38,572
  • 11
  • 72
  • 100
1
Need to override `equals()` and `hashCode()` method in the class `X`.

If not you can't get the correct true for x.clone().equals(x)

For

X x1 = new X()
x1.equals(x)

Since not overriding equals()

x1 and x are considered as same object of X class so it return true

Without overriding it will check the equals() in Object class

public boolean equals(Object obj) {
     return (this == obj);     
}
user123456
  • 21
  • 2
0

Try first to equals() and hashCode() and make thier result based on class attribute value so that when you override , the cloned instance should return the same value of hashCode() and the equals code should return true. check this it may help you .

Community
  • 1
  • 1
Mifmif
  • 2,887
  • 14
  • 21
0

First Question : x.clone().equals(x) It will return false.

We need override equals & hashcode.Because of the following reason we need to do.

1) Clone will create new instance of x. Reference of two instances are different since both are different reference.

2) If equals & hashcode methods are not overridden, Super class Object#equals will be invoked. This will check the memory location of the object. As per our earlier point both have different address, so it will false.

Second Question : X x1 = x x1.equals(x) return true.

1) x1,x are same object and same location.

2) So even override equals & hashcode methods are not overridden, it will check the memory location and will return true.

Siva Kumar
  • 1,990
  • 1
  • 12
  • 26