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.

public class Student
  int marks;
  public static void main(String args[])
    Student std1 = new Student();
    Student std2 = new Student();

    std1.marks = 75;
    std2 = std1;                        // object to object assignment
    System.out.println("std1 marks before change: " + td1.marks);  // 75 
    System.out.println("std2 marks before change: " + td2.marks);  // 75
    std1.marks = 85;
    System.out.println("std1 marks after change: " + std1.marks);  // 85 
    System.out.println("std2 marks after change: " + std2.marks);  // 85

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.

public class Officer
  int salary;
  public static void main(String args[])
    Officer o1 = new Officer();
    Officer o2 = new Officer();

    o1.salary = 5000;
    o2.salary = o1.salary;
    System.out.println("o1 salary before change: " + o1.salary); // 5000
    System.out.println("o2 salary before change: " + o2.salary); // 5000

    o1.salary = 6000;
    System.out.println("o1 salary after change: " + o1.salary); // 6000
    System.out.println("o2 salary after change: " + o2.salary); // 5000

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. 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

  2. how we identify whether program in shallow copying or deep copying .
    and according to above program,i didn’t find any difference between two except output .

Leave a Comment

Your email address will not be published.