Problem statement: Shallow copy vs Deep copy.
shallow = little depth
Shallow copy [meaning reference copy]
is method of copying an object and is followed by default in cloning. In this method the fields of an old object X are copied to the new object Y. While copying the object type field the reference is copied to Y i.e object Y will point to same location as pointed out by X. If the field value is a primitive type it copies the value of the primitive type
Therefore, any changes made in referenced objects in object X or Y will be reflected in other object
Deep copy [meaning object copy]
If we want to create a deep copy of object X and place it in a new object Y then new copy of any referenced objects fields are created and these references are placed in object Y. This means any changes made in referenced object fields in object X or Y will be reflected only in that object and not in the other. In below example, we create a deep copy of object.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
Explanation with program:
shallow = little depth
Shallow copy [meaning reference copy]
is method of copying an object and is followed by default in cloning. In this method the fields of an old object X are copied to the new object Y. While copying the object type field the reference is copied to Y i.e object Y will point to same location as pointed out by X. If the field value is a primitive type it copies the value of the primitive type
Therefore, any changes made in referenced objects in object X or Y will be reflected in other object
Deep copy [meaning object copy]
If we want to create a deep copy of object X and place it in a new object Y then new copy of any referenced objects fields are created and these references are placed in object Y. This means any changes made in referenced object fields in object X or Y will be reflected only in that object and not in the other. In below example, we create a deep copy of object.
A deep copy copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. A deep copy occurs when an object is copied along with the objects to which it refers.
Explanation with program:
import java.util.ArrayList;
class TestExe {
    int x, y;
}
// reference of TestExe and implements clone with deep copy
class Test2 implements Cloneable {
    int a, b;
    TestExe c = new TestExe();
    public Object clone() throws CloneNotSupportedException {
        // Assign the shallow copy to new reference variable t
        Test2 t = (Test2) super.clone();
        t.c = new TestExe();
        return t;
    }
}
public class DeepCloning {
    public static void main(String args[]) throws CloneNotSupportedException {
        Test2 t1 = new Test2();
        t1.a = 10;
        t1.b = 20;
        t1.c.x = 30;
        t1.c.y = 40;
        Test2 t2 = (Test2) t1.clone();
        t2.a = 100;
        // Change in primitive type of t2 will not be reflected in t1 field
        t2.c.x = 300;
        // Change in object type field of t2 will not be reflected in t1(deep copy)
        System.out.println(t1.a + " " + t1.b + " " + t1.c.x + " " + t1.c.y);
        System.out.println(t2.a + " " + t2.b + " " +t2.c.x + " " + t2.c.y);
    }
}
Output:
10 20 30 40
100 20 300 0
Output:
10 20 30 40
100 20 300 0
 
No comments:
Post a Comment