Why is the Object class's clone() method giving a deep copy of object?

前端 未结 4 1362
面向向阳花
面向向阳花 2021-01-22 10:25

As per the JAVA documentation, the super.clone() when called returns a shallow copy of the object. In the code below I have two objects name and id

4条回答
  •  迷失自我
    2021-01-22 10:37

    try this test

        Cloning obj = new Cloning("Annu",203, 1000);
        Cloning obj1 = (Cloning)obj.clone();
        System.out.println(obj.id == obj1.id);
    

    it prints true, it means id of cloned object points to the same instance of Integer, if it were deep clone it would print false

提交回复
热议问题