Difference between Shallow and Deep copy of a class

Last Updated : 11 Jun, 2026

In Java, copying an object can be done in two ways that is Shallow Copy and Deep Copy. Both techniques create a duplicate of an object, but they differ in how object references are handled. Understanding these concepts is important when working with objects that contain reference-type fields such as arrays, collections, or other objects.

  • Shallow Copy copies object references instead of creating new referenced objects.
  • Deep Copy creates completely independent copies of all referenced objects.
  • Changes in a shallow copy may affect the original object.

Shallow Copy

A Shallow Copy creates a new object but copies the references of nested objects instead of creating new copies of them. As a result, both the original and copied objects share the same referenced data. Any modification to shared objects is reflected in both copies.

  • Copies only the top-level object.
  • Referenced objects remain shared between copies.
  • Faster and memory-efficient.
shallowCopy
Shallow Copy

Syntax

ClassName copiedObject = (ClassName) originalObject.clone();

Java
import java.util.ArrayList;

class Car {
    String name;
    ArrayList<String> colors;

    Car(String name, ArrayList<String> colors) {
        this.name = name;
        this.colors = colors;
    }
}

public class Main {
    public static void main(String[] args) {

        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Blue");

        Car original = new Car("Honda", colors);

        // Shallow Copy
        Car copy = original;

        copy.colors.add("Green");

        System.out.println("Original Colors: " + original.colors);
        System.out.println("Copied Colors: " + copy.colors);
    }
}

Output
Original Colors: [Red, Blue, Green]
Copied Colors: [Red, Blue, Green]

Explanation :The variable copy refers to the same object as original. When "Green" is added through copy, the change is visible in original because both references point to the same object and collection.

Deep Copy

A Deep Copy creates a completely independent copy of an object, including all referenced objects. The copied object has its own memory allocation, so changes made to it do not affect the original object.

  • Creates new copies of referenced objects.
  • Original and copied objects are completely independent.
  • Safer when working with mutable objects.
DeepCopy
Deep Copy

Syntax

ClassName copiedObject = new ClassName(original.field1, new ReferencedObject(original.referenceField));

Java
import java.util.ArrayList;

class Car {
    String name;
    ArrayList<String> colors;

    Car(String name, ArrayList<String> colors) {
        this.name = name;
        this.colors = colors;
    }
}

public class Main {
    public static void main(String[] args) {

        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Blue");

        Car original = new Car("Honda", colors);

        // Deep Copy
        Car copy = new Car(
                original.name,
                new ArrayList<>(original.colors));

        copy.colors.add("Green");

        System.out.println("Original Colors: " + original.colors);
        System.out.println("Copied Colors: " + copy.colors);
    }
}

Output
Original Colors: [Red, Blue]
Copied Colors: [Red, Blue, Green]

Explanation: A new ArrayList is created for the copied object. Therefore, when "Green" is added to the copied object's list, the original object's list remains unchanged.

Shallow Vs Deep copy

FeatureShallow CopyDeep Copy
DefinitionCreates a new object but copies references of nested objects.Creates a new object and copies all nested objects as well.
Object ReferencesReferences are shared between original and copied objects.References point to newly created independent objects.
Memory AllocationLess memory is used because referenced objects are not duplicated.More memory is used because all objects are duplicated.
Effect of ChangesChanges in referenced objects are reflected in both copies.Changes in copied objects do not affect the original object.
Copy LevelCopies only the top-level object.Copies the complete object hierarchy.
PerformanceFaster because fewer objects are created.Slower because additional objects must be created.
Implementation ComplexityEasy to implement.More complex to implement.
Default clone() BehaviorObject.clone() performs a shallow copy by default.Requires overriding clone() and manually copying referenced objects.
Data IndependenceOriginal and copied objects are partially dependent.Original and copied objects are completely independent.
Mutable ObjectsNot suitable when objects contain mutable references.Suitable for mutable objects and collections.
Risk of Side EffectsHigh, because shared references can modify original data.Low, because each object has its own copy of data.
Collection HandlingCollections are shared between objects.Separate copies of collections are created.
Use CaseWhen sharing data is acceptable and memory usage should be minimized.When complete data isolation is required.
Object GraphCreates a partial copy of the object graph.Creates a complete copy of the object graph.
Data SafetyLess safe due to shared references.More safe because data is independent.
Comment