Table of contents
1.
Introduction
2.
Methods of Copying / Cloning
2.1.
What is Shallow Copy/ Cloning in Java
2.2.
What is Deep Copy / Cloning in Java
3.
What is Cloning in Java?
4.
Why Object Cloning is Important in Java Development
4.1.
When Cloning Becomes Useful
4.1.1.
a. Creating Copies of Configured Objects
4.1.2.
b. Reducing Overhead in Object Initialization
4.1.3.
c. Supporting Undo Mechanisms or History Tracking
4.2.
Deep vs Shallow Cloning: Key Differences
4.2.1.
1. Core Conceptual Differences
4.2.2.
2. Comparison Table: Shallow vs Deep Cloning
4.2.3.
3. Performance Impacts of Each Approach
5.
Frequently Asked Questions
5.1.
What is the difference between shallow copying & deep copying?
5.2.
When should I use shallow copying vs deep copying?
5.3.
Can I use the clone() method for deep copying?
6.
Conclusion
Last Updated: Sep 23, 2025
Medium

Deep Cloning and Shallow Cloning in Java

Author Rahul Singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Copying objects is a basic concept in Java that every developer should learn. When we create a copy of an object, we are creating a new object with the same state as the original. Java provides two main approaches for copying objects: shallow copying & deep copying. Shallow copying creates a new object that shares the same references as the original object, while deep copying creates a completely independent copy of the object & all its associated objects. In addition to these two methods, Java also supports the concept of cloning, which allows us to create a copy of an object using the clone() method. 

Deep Cloning and Shallow Cloning in Java

In this article, we will discuss the concepts of shallow copying, deep copying, & cloning in Java. We will also look at proper examples to understand these concepts.

Methods of Copying / Cloning

In Java, there are two primary methods for copying objects: shallow copying & deep copying. Let's explore each of these methods in more detail.

What is Shallow Copy/ Cloning in Java

A shallow copy creates a new object that shares the same references as the original object. In other words, the new object points to the same memory locations as the original object. Any changes made to the original object will be reflected in the copied object, & vice versa. Shallow copying is the default behavior when assigning one object to another or when using the clone() method without any modification.

For example : 

class ShallowCopy {
    private int[] data;

    public ShallowCopy(int[] values) {
        data = values;
    }

    public void printData() {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }
}

public class Main {
    public static void main(String[] args) {
        int[] values = {1, 2, 3, 4, 5};
        ShallowCopy obj1 = new ShallowCopy(values);
        ShallowCopy obj2 = obj1;

        obj1.printData(); 
        obj2.printData(); 

        values[0] = 10;

        obj1.printData(); 
        obj2.printData(); 
    }
}
You can also try this code with Online Java Compiler
Run Code


Output: 

1 2 3 4 5
1 2 3 4 5
10 2 3 4 5
10 2 3 4 5


In this example, `obj1` & `obj2` are shallow copies of each other. When we modify the `values` array, both `obj1` & `obj2` reflect the changes since they share the same reference to the array.

What is Deep Copy / Cloning in Java

Unlike shallow copying, deep copying creates a completely independent copy of the original object & all its associated objects. In a deep copy, the new object has its own set of references, & any changes made to the original object will not affect the copied object, & vice versa. Deep copying is useful when you want to create a true standalone copy of an object.

To achieve deep copying in Java, you need to manually create a new object & copy all the fields from the original object to the new object. If the object contains nested objects, you need to recursively create deep copies of those objects as well.

For example : 

class DeepCopy {
    private int[] data;

    public DeepCopy(int[] values) {
        data = new int[values.length];
        for (int i = 0; i < values.length; i++) {
            data[i] = values[i];
        }
    }

    public void printData() {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }
}

public class Main {
    public static void main(String[] args) {
        int[] values = {1, 2, 3, 4, 5};
        DeepCopy obj1 = new DeepCopy(values);
        DeepCopy obj2 = new DeepCopy(obj1.data);

        obj1.printData(); 
        obj2.printData(); 

        values[0] = 10;

       obj1.printData(); 
        obj2.printData(); 
    }
}
You can also try this code with Online Java Compiler
Run Code


Output: 

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5


In this example, `obj1` & `obj2` are deep copies of each other. The `DeepCopy` constructor creates a new array & copies the values from the original array to the new array. When we modify the `values` array, only `obj1` reflects the changes since it has its own independent copy of the data.

What is Cloning in Java?

Cloning is another way to create a copy of an object in Java. It is a built-in mechanism that allows you to create a new object with the same state as the original object. To use cloning, the class must implement the `Cloneable` interface & override the `clone()` method.

For example : 

class Cloning implements Cloneable {
    private int[] data;

    public Cloning(int[] values) {
        data = values;
    }

    public void printData() {
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i] + " ");
        }
        System.out.println();
    }

    @Override
    public Cloning clone() throws CloneNotSupportedException {
        Cloning cloned = (Cloning) super.clone();
        cloned.data = data.clone();
        return cloned;
    }
}
public class Main {
    public static void main(String[] args) {
        int[] values = {1, 2, 3, 4, 5};
        Cloning obj1 = new Cloning(values);

        try {
            Cloning obj2 = obj1.clone();

            obj1.printData(); 
            obj2.printData(); 

            values[0] = 10;

            obj1.printData(); 
            obj2.printData(); 
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
You can also try this code with Online Java Compiler
Run Code


Output: 

1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5


In this example, the `Cloning` class implements the `Cloneable` interface & overrides the `clone()` method. Inside the `clone()` method, we first call `super.clone()` to create a shallow copy of the object. Then, we manually create a deep copy of the `data` array using `data.clone()`.

Note: When we invoke `obj1.clone()`, it creates a new object `obj2` with the same state as `obj1`. Any changes made to the original object or its associated objects will not affect the cloned object.

Why Object Cloning is Important in Java Development

Object cloning in Java is a powerful technique that allows developers to create duplicate objects with the same internal state. It becomes particularly useful when you need to preserve an object's current configuration without building it again from scratch. The clone() method, part of Java’s Object class, supports this capability.

Cloning improves development efficiency by avoiding repetitive initialization logic and enables better memory and time management. It's also fundamental in implementing design patterns like Prototype, where new objects are created by copying existing ones. In real-world Java development, cloning reduces boilerplate code and ensures consistency in object duplication.

When Cloning Becomes Useful

a. Creating Copies of Configured Objects

Suppose you have a fully-configured object like a template or a database connection with predefined settings. Instead of creating and configuring it again, cloning allows you to make a copy instantly. This saves development time and ensures consistency.

b. Reducing Overhead in Object Initialization

Some objects require expensive initialization (e.g., reading from a file or network). Cloning such objects avoids the cost of repeated setup. For instance, in a simulation app, you can clone a pre-set configuration object instead of rebuilding it.

c. Supporting Undo Mechanisms or History Tracking

Cloning is commonly used in applications that need undo functionality or version tracking. Before an operation changes an object, a clone of its previous state is saved. This way, the system can restore the earlier version when needed.

Deep vs Shallow Cloning: Key Differences

1. Core Conceptual Differences

In shallow cloning, the cloned object gets a new top-level object, but the fields (especially objects inside it) are shared by reference. This means changes in the original's nested objects reflect in the clone.

In contrast, deep cloning creates copies of all nested objects, resulting in a fully independent duplicate. This prevents unexpected side effects when modifying either object.

2. Comparison Table: Shallow vs Deep Cloning

FeatureShallow CloningDeep Cloning
Memory UsageLowerHigher due to recursive copying
ComplexitySimple (uses clone())More complex (custom logic or libraries)
PerformanceFastSlower due to deep traversal
Use CasesWhen internal objects are immutableWhen nested objects need to be independent
Mutability ImpactAffects both original and clone if mutableCompletely isolated objects

3. Performance Impacts of Each Approach

Shallow cloning is faster and less memory-intensive, making it suitable for lightweight objects or when internal fields are immutable. However, it's risky if nested objects are mutable—changes in one will affect the other unintentionally.

Deep cloning provides safety and data isolation but requires more processing time and memory. It’s ideal when object independence is critical, such as in multithreaded applications or undo-redo systems.

Understanding these trade-offs is essential for optimizing performance and avoiding bugs.

By understanding object cloning in Java, developers can improve code efficiency, reduce duplication, and build more robust, maintainable systems.

Frequently Asked Questions

What is the difference between shallow copying & deep copying?

Shallow copying creates a new object that shares the same references as the original object, while deep copying creates a completely independent copy of the object & all its associated objects.

When should I use shallow copying vs deep copying?

Shallow copying is useful when you want to create a new object that refers to the same underlying data as the original object. Deep copying is necessary when you want to create a true standalone copy of an object that is entirely independent of the original.

Can I use the clone() method for deep copying?

By default, the clone() method performs a shallow copy. However, you can override the clone() method to create a deep copy by manually copying the object's fields & recursively cloning any nested objects.

Conclusion

In this article, we discussed the concepts of shallow copying, deep copying, & cloning in Java. We learned that shallow copying creates a new object with shared references, while deep copying creates a completely independent copy. We also explained how to use the clone() method to create copies of objects

Live masterclass