Shallow Copying Vs Deep Copying Copying an Object

Shallow Copying Vs Deep Copying Copying an Object


Shallow Copying Vs Deep Copying: Copying an object involves getting another object with the same properties of the original. Here, there exists two ways – two objects having their own set of properties (instance variables) or both objects referring the same location of properties. Following gives the detailed discussion.

Shallow Copying

Shallow copying is the easier of the two styles; here, one object is assigned with another. When assigned, both objects refer the same location of variables. When both objects refer or share the same location, the minus point is if one object changes the value, the other object also gets affected. That is, to say straight is no encapsulation exists. One small advantage is memory is less used.

Following is the example on shallow copying.


Shallow Copying Vs Deep Copying
Output screenshot of Shallow Copying Vs Deep Copying

std1 and std2 are the two objects of Student class. Student object std1 binds with an instance variable marks with a value of 75 (we call marks is a property of Student).

std2 = std1;

In the above statement, object std2 is assigned with std1. Java rule is, when two objects are assigned, they refer the same location of variables (other way, this is how Java achieves pointer concept). The two objects std1 and std2 refer the same location of marks. The bad affect is if one object changes the value, the other is also gets affected. For this reason, when std1.marks is assigned with a new value of 85, std2.marks is also gets changed; observe the screenshot.

Assigning object to object is known as shallow copying. Even though it is easier, no encapsulation exists.

Deep Copying

The minus of point of shallow copying is over come in deep copying. In deep copying, we assign variable by variable of one object to the other. Even though, it is tedious, encapsulation exists . Following program explains.


Shallow Copying Vs Deep Copying
Output screenshot of Shallow Copying Vs Deep Copying

To make the program simple, only one property is given to Officer, salary. Two objects o1 and o2 are created.

o2.salary = o1.salary;

In the above statement, salary of o1 is assigned to the salary of o2. Here, variable to variable is assigned (in the earlier program, object to object is assigned). In this case, encapsulation is maintained as both objects have their separate locations for salary variable. One object does not have any relation (connection) with the other. For this reason, as you can observe in the above screenshot, when o1 salary is changed to 6000, o2.salary did not get affect. You can also try with o2 salary and observe o1 salary does not change.

When to choose which
As a programmer, when to prefer which. When no encapsulation is required prefer shallow copying and when required, prefer deep copying.

Java Cloning

Another copying exists in Java called cloning. Cloning gives the affect of deep copying with shallow copying.

4 thoughts on “Shallow Copying Vs Deep Copying Copying an Object

  1. ravi raj

    i don’t understand how can many refernce variable points to single object explicitly in assigning object to class variable
    eg:question q=new question();
    question q1=q;
    here q1 is object is created are it is classvariable simply to point reference of q object

Leave a Reply

Your email address will not be published. Required fields are marked *