1.

Explain shallow copy vs deep copy in the context of Java.

Answer»

The method of copying an object that is used by default in cloning is a shallow copy. The fields of an old object X are copied to a new object Y in this procedure. The reference is duplicated to Y while copying the object type field, i.e. object Y will point to the same location as X. If the field value is a primitive type, the primitive type's value is copied. As a result, any changes made in object X or Y's linked objects will be REFLECTED in other objects.

// Java// An object reference of this class is// contained by IBSclass IB { int a, b;} // Contains a reference of IB and// implements clone with shallow copy.class IBS implements Cloneable { int x; int y; IB z = new IB(); public Object clone() throws CloneNotSupportedException { return super.clone(); }}// Driver classpublic class Main { public static void main(String ARGS[]) throws CloneNotSupportedException { IBS t1 = new IBS(); t1.x = 1; t1.y = 2; t1.z.a = 3; t1.z.b = 4; IBS t2 = (IBS)t1.clone(); // a copy of object t1 is CREATED // and is PASSED to t2 t2.x = 10; // any modification in the primitive type of t2 // does not get reflected in the t1 field t2.z.a = 30; // any modification in object type field // gets reflected in both t2 and t1(shallow copy) System.out.println(t1.x + " " + t1.y + " " + t1.z.a + " " + t1.z.b); System.out.println(t2.x + " " + t2.y + " " + t2.z.a + " " + t2.z.b); }}

Output:

1 2 30 410 2 30 4

In the above PROGRAM, t1.clone returns a shallow copy of the object t1. After receiving the copy, specific modifications to the clone technique must be applied in order to acquire a deep copy of the object.

A deep copy duplicates all fields as well as the dynamically allocated memory that the fields point to. When an item is copied together with the objects to which it refers, it is called a deep copy.

// Java// An object reference of this// class is contained by IBSclass IB { int a, b;} // Contains a reference of IB and// implements clone with deep copy.class IBS implements Cloneable { int x, y; IB z = new IB(); public Object clone() throws CloneNotSupportedException { // Assigning the shallow copy to // the new reference variable t IBS t = (IBS)super.clone(); // Creating a deep copy for c t.z = new IB(); t.z.a = z.a; t.z.b = z.b; // Creating a new object for the field c // and assigning it to the obtained shallow copy // in order to make it a deep copy return t; }}public class Main { public static void main(String args[]) throws CloneNotSupportedException { IBS t1 = new IBS(); t1.x = 1; t1.y = 2; t1.z.a = 3; t1.z.b = 4; IBS t3 = (IBS)t1.clone(); t3.x = 10; // any modification in the primitive type of t2 // does not get reflected in the t1 field t3.z.a = 30; // any modification in object type field of t2 // does not get reflected in t1(deep copy) System.out.println(t1.x + " " + t1.y + " " + t1.z.a + " " + t1.z.b); System.out.println(t3.x + " " + t3.y + " " + t3.z.a + " " + t3.z.b); }}

Output:

1 2 3 410 2 30 4

We can see how a new object for the IB class has been designated to replicate an object that will be delivered to the clone method in the preceding example. As a result, t3 will acquire a deep copy of t1's object. As a result, any changes made by t3 to the ‘z' object properties will not be reflected in t1.



Discussion

No Comment Found