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.

Syntax
ClassName copiedObject = (ClassName) originalObject.clone();
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.

Syntax
ClassName copiedObject = new ClassName(original.field1, new ReferencedObject(original.referenceField));
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
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Definition | Creates a new object but copies references of nested objects. | Creates a new object and copies all nested objects as well. |
| Object References | References are shared between original and copied objects. | References point to newly created independent objects. |
| Memory Allocation | Less memory is used because referenced objects are not duplicated. | More memory is used because all objects are duplicated. |
| Effect of Changes | Changes in referenced objects are reflected in both copies. | Changes in copied objects do not affect the original object. |
| Copy Level | Copies only the top-level object. | Copies the complete object hierarchy. |
| Performance | Faster because fewer objects are created. | Slower because additional objects must be created. |
| Implementation Complexity | Easy to implement. | More complex to implement. |
| Default clone() Behavior | Object.clone() performs a shallow copy by default. | Requires overriding clone() and manually copying referenced objects. |
| Data Independence | Original and copied objects are partially dependent. | Original and copied objects are completely independent. |
| Mutable Objects | Not suitable when objects contain mutable references. | Suitable for mutable objects and collections. |
| Risk of Side Effects | High, because shared references can modify original data. | Low, because each object has its own copy of data. |
| Collection Handling | Collections are shared between objects. | Separate copies of collections are created. |
| Use Case | When sharing data is acceptable and memory usage should be minimized. | When complete data isolation is required. |
| Object Graph | Creates a partial copy of the object graph. | Creates a complete copy of the object graph. |
| Data Safety | Less safe due to shared references. | More safe because data is independent. |