---
title: "Discuss the differences between deep copy and shallow copy in Java objects."  
description: "Discuss the differences between deep copy and shallow copy in Java objects."  
author: "Ravi Vishwakarma"  
published: 2024-07-17  
updated: 2024-07-17  
canonical: https://www.mindstick.com/interview/33945/discuss-the-differences-between-deep-copy-and-shallow-copy-in-java-objects  
category: "java"  
tags: ["java", "javac"]  
reading_time: 6 minutes  

---

# Discuss the differences between deep copy and shallow copy in Java objects.

> **In Shallow copy, a copy of the original object is stored and only the reference address is finally copied.** **In Deep copy, the copy of the original object and the repetitive copies both are stored**.

![Discuss the differences between deep copy and shallow copy in Java objects.](https://www.mindstick.com/interviewquestion/418ac4b6-e3c8-4e2c-b2fd-ce03d248b7bc/images/984a40f2-ac0e-41d7-85af-5d4021a67cdc.jpg)

#### Shallow Copy

A shallow copy of an object is a new object whose fields are the same as the original object. However, if the field is a reference to an object, the reference is copied, but the referred object is not. Consequently, both the original object and the shallow copy will reference the same nested objects.

#### Characteristics of Shallow Copy:

1. **Field-by-Field Copying:** The primitive fields (like `int`, `char`, etc.) are copied directly.
2. **Shared References:** For object fields, only the reference is copied, not the object itself. Therefore, changes to the referred objects affect both the original and the copied objects.
3. **Default Behavior:** The default clone method in Java's `Object` the class performs a shallow copy.

## Example:

```java
class Address {
    String city;
    Address(String city) {
        this.city = city;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // Shallow copy
    }
}

public class ShallowCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address("New York");
        Person person1 = new Person("John", address);
        Person person2 = (Person) person1.clone();

        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: New York

        person2.address.city = "Los Angeles";
        System.out.println(person1.address.city); // Output: Los Angeles
        System.out.println(person2.address.city); // Output: Los Angeles
    }
}
```

####

#### Deep Copy

A deep copy of an object involves creating a new object and then recursively copying all objects referenced by the original object. Consequently, changes to the objects referenced by the original object do not affect the deep copy.

#### Characteristics of Deep Copy:

1. **Independent Copies:** All objects referenced by the original object are also copied, resulting in a completely independent copy.
2. **Recursive Copying:** Requires implementing cloning for each referenced object to ensure complete independence.
3. **Custom Implementation:** Java does not provide a default deep copy method, so it must be implemented manually.

## Example:

```java
class Address implements Cloneable {
    String city;
    Address(String city) {
        this.city = city;
    }

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

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    protected Object clone() throws CloneNotSupportedException {
        Person cloned = (Person) super.clone();
        cloned.address = (Address) address.clone(); // Deep copy
        return cloned;
    }
}

public class DeepCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address("New York");
        Person person1 = new Person("John", address);
        Person person2 = (Person) person1.clone();

        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: New York

        person2.address.city = "Los Angeles";
        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: Los Angeles
    }
}
```

## Summary

- **Shallow Copy:** Only copies primitive fields and references to objects, leading to shared references.
- **Deep Copy:** Recursively copies all objects, resulting in completely independent copies.

## Read more

[**What is the difference between JDK, JRE, and JVM?**](https://www.mindstick.com/interview/33943/what-is-the-difference-between-jdk-jre-and-jvm)

[**Bitwise AND, OR, XOR, and left/right shift operations.**](https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations)

## Answers

### Answer by Ravi Vishwakarma

> **In Shallow copy, a copy of the original object is stored and only the reference address is finally copied.** **In Deep copy, the copy of the original object and the repetitive copies both are stored**.

![Discuss the differences between deep copy and shallow copy in Java objects.](https://www.mindstick.com/interviewquestion/418ac4b6-e3c8-4e2c-b2fd-ce03d248b7bc/images/984a40f2-ac0e-41d7-85af-5d4021a67cdc.jpg)

#### Shallow Copy

A shallow copy of an object is a new object whose fields are the same as the original object. However, if the field is a reference to an object, the reference is copied, but the referred object is not. Consequently, both the original object and the shallow copy will reference the same nested objects.

#### Characteristics of Shallow Copy:

1. **Field-by-Field Copying:** The primitive fields (like `int`, `char`, etc.) are copied directly.
2. **Shared References:** For object fields, only the reference is copied, not the object itself. Therefore, changes to the referred objects affect both the original and the copied objects.
3. **Default Behavior:** The default clone method in Java's `Object` the class performs a shallow copy.

## Example:

```java
class Address {
    String city;
    Address(String city) {
        this.city = city;
    }
}

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    protected Object clone() throws CloneNotSupportedException {
        return super.clone(); // Shallow copy
    }
}

public class ShallowCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address("New York");
        Person person1 = new Person("John", address);
        Person person2 = (Person) person1.clone();

        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: New York

        person2.address.city = "Los Angeles";
        System.out.println(person1.address.city); // Output: Los Angeles
        System.out.println(person2.address.city); // Output: Los Angeles
    }
}
```

####

#### Deep Copy

A deep copy of an object involves creating a new object and then recursively copying all objects referenced by the original object. Consequently, changes to the objects referenced by the original object do not affect the deep copy.

#### Characteristics of Deep Copy:

1. **Independent Copies:** All objects referenced by the original object are also copied, resulting in a completely independent copy.
2. **Recursive Copying:** Requires implementing cloning for each referenced object to ensure complete independence.
3. **Custom Implementation:** Java does not provide a default deep copy method, so it must be implemented manually.

## Example:

```java
class Address implements Cloneable {
    String city;
    Address(String city) {
        this.city = city;
    }

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

class Person implements Cloneable {
    String name;
    Address address;

    Person(String name, Address address) {
        this.name = name;
        this.address = address;
    }

    protected Object clone() throws CloneNotSupportedException {
        Person cloned = (Person) super.clone();
        cloned.address = (Address) address.clone(); // Deep copy
        return cloned;
    }
}

public class DeepCopyExample {
    public static void main(String[] args) throws CloneNotSupportedException {
        Address address = new Address("New York");
        Person person1 = new Person("John", address);
        Person person2 = (Person) person1.clone();

        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: New York

        person2.address.city = "Los Angeles";
        System.out.println(person1.address.city); // Output: New York
        System.out.println(person2.address.city); // Output: Los Angeles
    }
}
```

## Summary

- **Shallow Copy:** Only copies primitive fields and references to objects, leading to shared references.
- **Deep Copy:** Recursively copies all objects, resulting in completely independent copies.

## Read more

[**What is the difference between JDK, JRE, and JVM?**](https://www.mindstick.com/interview/33943/what-is-the-difference-between-jdk-jre-and-jvm)

[**Bitwise AND, OR, XOR, and left/right shift operations.**](https://www.mindstick.com/interview/33944/bitwise-and-or-xor-and-left-right-shift-operations)


---

Original Source: https://www.mindstick.com/interview/33945/discuss-the-differences-between-deep-copy-and-shallow-copy-in-java-objects

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
