Shallow copy and deep copy in java

Before discussing differences between shallow copy and deep copy let’s discuss what a copy is? We can have two types of copies:

  1. Reference copy: It creates a copy of a reference variable pointing to an object.
  2. Object copy: It creates a copy of the object itself.

Shallow copy

Shallow means having little depth.

Shallow copy of an object create a new object and copies all the field of object to the new object (either it is primitive or a reference). In case of non-primitive types only reference will be copied to the new instance. Therefore both original object and new object will point to the same non-primitive type fields i.e. objects. It returns an instance of Object class we have to explicitly cast to the original object.

Let’s consider the below example, we have a Customer class which have two properties name and address. In case of shallow copy a new Customer object will be created and as both name, address fields are of non-primitive type there references will be copied to new object. Now both the original object and copied object will point to the same name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects.

public class Customer implements Cloneable {
    private String name;
    private Address address;
    public Customer(String name, Address address) {
         this.name = name;
         this.address = address;
    }
    
    //Getter Setter
    
    Public Object clone throws CloneNotSupportedException {
	return super.clone();
    }    
}

Deep copy

Deep copy of an object create a fully independent copy of an object. In case of deep copy everything will be copied to the new object. After deep copy, original object and copied object will refers to the different primitive and non-primitive fields. Changes on one object’s fields will not reflects in other’s.

Let’s consider the same example which we discussed earlier. We have a Customer class which have two properties name and address. In case of deep copy a new independent Customer object will be created. Now the original object and copied object will point to the different name and address objects.

Here what is important that in case of name values will not reflect in other if we change it in one object. This is because of immutable nature of string objects. So we do not have to do explicit cloning for that.

public class Customer implements Cloneable {
    private String name;
    private Address address;
    public Customer(String name, Address address) {
         this.name = name;
         this.address = address;
    }
    
    //Getter Setter

    //Override clone method
    @Override
    Public Object clone throws CloneNotSupportedException {
	Customer clonedObj = (Customer)super.clone();
	clonadObj.address = this.address.clone();
	return clonedObj;
       }	
}

Note:

  • clone() method of the object class support shallow copy of the object.
  • In case of deep copy we have to override the clone() method of the object class and we also need to ensure that all the member class also implement the Cloneable interface.
  • There is no difference between shallow and deep copy in java if only primitive type fields or Immutable objects are there as fields in the original object.