Open In App

Deep, Shallow and Lazy Copy with Java Examples

In object-oriented programming, object copying is creating a copy of an existing object, the resulting object is called an object copy or simply copy of the original object.There are several ways to copy an object, most commonly by a copy constructor or cloning.
We can define Cloning as “create a copy of object”. Shallow, deep and lazy copy is related to cloning process. 
These are actually three ways for creating copy object.
Shallow Copy 
 

 






//code illustrating shallow copy
public class Ex {
  
    private int[] data;
  
    // makes a shallow copy of values
    public Ex(int[] values) {
        data = values;
    }
  
    public void showData() {
        System.out.println( Arrays.toString(data) );
    }
}

The above code shows shallow copying. data simply refers to the same array as vals.
 



This can lead to unpleasant side effects if the elements of values are changed via some other reference.
 




public class UsesEx{
  
    public static void main(String[] args) {
        int[] vals = {3, 7, 9};
        Ex e = new Ex(vals);
        e.showData(); // prints out [3, 7, 9]
        vals[0] = 13;
        e.showData(); // prints out [13, 7, 9]
  
        // Very confusing, because we didn't
        // intentionally change anything about 
        // the object e refers to.
    }
}

Output 1 : [3, 7, 9]
Output 2 : [13, 7, 9]

Deep Copy 
 

A deep copy means actually creating a new array and copying over the values.
 




// Code explaining deep copy
public class Ex {
      
    private int[] data;
  
    // altered to make a deep copy of values
    public Ex(int[] values) {
        data = new int[values.length];
        for (int i = 0; i < data.length; i++) {
            data[i] = values[i];
        }
    }
  
    public void showData() {
        System.out.println(Arrays.toString(data));
    }
}






public class UsesEx{
  
    public static void main(String[] args) {
        int[] vals = {3, 7, 9};
        Ex e = new Ex(vals);
        e.showData(); // prints out [3, 7, 9]
        vals[0] = 13;
        e.showData(); // prints out [3, 7, 9]
  
       // changes in array values will not be 
       // shown in data values. 
    }
}

Output 1 : [3, 7, 9]
Output 2 : [3, 7, 9]

Changes to the array vals will not result in changes to the array data.
when to use what 
There is no hard and fast rule defined for selecting between shallow copy and deep copy but normally we should keep in mind that if an object has only primitive fields, then obviously we should go for shallow copy, but if the object has references to other objects, then based on the requirement, shallow copy or deep copy should be done. If the references are not updated then there is no point to initiate a deep copy.
Lazy Copy 
A lazy copy can be defined as a combination of both shallow copy and deep copy. The mechanism follows a simple approach – at the initial state, shallow copy approach is used. A counter is also used to keep a track on how many objects share the data. When the program wants to modify the original object, it checks whether the object is shared or not. If the object is shared, then the deep copy mechanism is initiated.
Summary 
In shallow copy, only fields of primitive data type are copied while the objects references are not copied. Deep copy involves the copy of primitive data type as well as object references. There is no hard and fast rule as to when to do shallow copy and when to do a deep copy. Lazy copy is a combination of both of these approaches


Article Tags :