Core Java

Shallow Copy and Deep Copy – Java

Before diving into the concept of shallow copy and deep copy in Java, it’s essential to understand the concept of object copying itself. In programming, when we talk about copying objects, we refer to creating a new object with the same values as an existing object. This is often necessary when we want to modify or manipulate an object without affecting the original one.

There are two common approaches to object copying in Java: shallow copy and deep copy. In this guide, we will explore the differences between these two techniques, understand their implications, and provide code examples in Java to illustrate their behavior.

1. Introduction

When copying an object, we aim to create a new object that is independent of the original object. The primary distinction between shallow copy and deep copy lies in how they handle object references.

In a shallow copy, the new object is created, but the references to the object’s fields are copied rather than the actual values. This means that changes made to the copied object’s fields will be reflected in the original object and vice versa. Shallow copy creates a new object that shares references with the original object.

On the other hand, a deep copy creates a completely independent object with its own set of values. All fields, including any objects referenced by the original object, are duplicated in the new object. Deep copy ensures that any modifications made to the copied object will not affect the original object.

The choice between shallow copy and deep copy depends on the requirements of your program and the nature of the objects being copied. Let’s examine these concepts in more detail and see how they are implemented in Java.

2. Shallow Copy

2.1 Overview

Shallow copy, as mentioned earlier, creates a new object but copies the references to the fields of the original object. This means that any changes made to the copied object’s fields will be reflected in the original object and vice versa. In essence, the new object shares the same memory addresses as the original object’s fields.

2.2 Shallow Copy Implementation in Java

In Java, creating a shallow copy of an object is straightforward. We can utilize the clone() method from the Cloneable interface, which provides a default shallow copy implementation.

Here’s an example that demonstrates the shallow copy concept:

class Employee implements Cloneable {
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

public class ShallowCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Employee original = new Employee("John Doe", 30);
        Employee copy = (Employee) original.clone();

        System.out.println("Original: " + original.getName() + ", " + original.getAge());
        System.out.println("Copy: " + copy.getName() + ", " + copy.getAge());

        copy.setName("Jane Smith");
        copy.setAge(25);

        System.out.println("Original after modification: " + original.getName() + ", " + original.getAge());
        System.out.println("Copy after modification: " + copy.getName() + ", " + copy.getAge());
    }
}

In this example, we have an Employee class with a name and age field. The Employee class implements the Cloneable interface and overrides the clone() method to provide a shallow copy implementation. We create an original Employee object and clone it to obtain a copy.

Fig. 1: Shallow Copy in Java Example Output.
Fig. 1: Shallow Copy in Java Example Output.

Upon modifying the fields of the copied object, we observe that the changes are also reflected in the original object. This behavior confirms that shallow copy creates a new object with references to the original object’s fields.

3. Deep Copy

3.1 Overview

Unlike shallow copy, deep copy creates a new object with its own set of values. All fields, including any objects referenced by the original object, are duplicated in the new object. Changes made to the copied object’s fields will not affect the original object.

3.2 Deep Copy Implementation in Java

Deep copy in Java requires a more explicit implementation compared to shallow copy. We need to manually create new instances of any referenced objects and copy their values as well. This ensures that changes made to the copied object will not affect the original object or its referenced objects.

Let’s modify the previous example to demonstrate deep copy:

class Employee implements Cloneable {
    private String name;
    private int age;

    public Employee(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Employee(Employee employee) {
        this.name = employee.name;
        this.age = employee.age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new Employee(this);
    }
}

public class DeepCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Employee original = new Employee("John Doe", 30);
        Employee copy = (Employee) original.clone();

        System.out.println("Original: " + original.getName() + ", " + original.getAge());
        System.out.println("Copy: " + copy.getName() + ", " + copy.getAge());

        copy.setName("Jane Smith");
        copy.setAge(25);

        System.out.println("Original after modification: " + original.getName() + ", " + original.getAge());
        System.out.println("Copy after modification: " + copy.getName() + ", " + copy.getAge());
    }
}

In this updated example, we have added an additional constructor to the Employee class that takes an existing Employee object and copies its values. This allows us to create a new Employee object with the same values as the original object.

Fig. 2: Deep Copy in Java Example Output.
Fig. 2: Deep Copy in Java Example Output.

Now, upon modifying the fields of the copied object, we observe that the changes do not affect the original object. This behavior confirms that deep copy creates a new object with its own set of values, independent of the original object.

3. Conclusion

Understanding the differences between shallow copy and deep copy is crucial when dealing with object copying in programming. Shallow copy creates a new object with references to the fields of the original object, while deep copy creates a new object with its own set of values, including any referenced objects.

When deciding which approach to use, consider the requirements of your program. Shallow copy may be sufficient if you want the copied object to reflect changes made to the original object. However, if you need an independent copy that does not affect the original object, deep copy is the way to go.

Remember that the implementation of deep copy requires careful consideration of all fields, especially those that refer to other objects. By understanding these concepts and utilizing the appropriate technique, you can effectively manage object copying in your Java applications.

Download the Source Code

This was an example of Shallow Copy and Deep Copy in Java!

Download
You can download the full source code of this example here: Shallow Copy and Deep Copy – Java

Odysseas Mourtzoukos

Mourtzoukos Odysseas is studying to become a software engineer, at Harokopio University of Athens. Along with his studies, he is getting involved with different projects on gaming development and web applications. He is looking forward to sharing his knowledge and experience with the world.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button