What is the Difference Between Shallow Copy and Deep Copy in Java?
When working with objects in Java, especially in real-time applications, understanding object copying is very important. One common interview question is:
What is the difference between Shallow Copy and Deep Copy?
Let’s understand this clearly with examples.
🔹 What is a Shallow Copy?
A shallow copy creates a new object, but it copies the references of nested objects instead of creating new copies of them.
👉 That means both original and copied objects point to the same memory location for internal objects.
🔹 Example of Shallow Copy
class Address {
String city;
Address(String city) {
this.city = city;
}
}
class Employee implements Cloneable {
int id;
Address address;
Employee(int id, Address address) {
this.id = id;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException {
return super.clone(); // Shallow Copy
}
}
What Happens?
A new
Employeeobject is created.But the
Addressobject is shared.If you modify the address in one object, it affects the other.
👉 Both objects share the same nested object.
🔹 What is a Deep Copy?
A deep copy creates a new object and also creates separate copies of all referenced objects.
👉 No shared references. Everything is copied independently.
🔹 Example of Deep Copy
class Employee implements Cloneable {
int id;
Address address;
Employee(int id, Address address) {
this.id = id;
this.address = address;
}
protected Object clone() throws CloneNotSupportedException {
Address newAddress = new Address(this.address.city);
return new Employee(this.id, newAddress); // Deep Copy
}
}
What Happens?
New
Employeeobject is created.New
Addressobject is also created.Changes in one object do NOT affect the other.
🔥 Key Differences
🔹 Real-Time Example
Imagine a banking system:
Account account1 = new Account(balanceDetails);
Account account2 = account1.clone();
If it’s a shallow copy:
Updating balance in one account may affect the other.
If it’s a deep copy:
Each account works independently.
In enterprise applications, deep copy is usually preferred when dealing with complex object graphs.
🔹 How to Implement Deep Copy in Java?
You can implement deep copy using:
✔ Overriding clone() method
✔ Copy constructor
✔ Serialization & Deserialization
✔ Manual copying of fields
🔹 Interview Follow-Up Questions
Interviewers may ask:
Does
Object.clone()perform shallow or deep copy?How does cloning work internally?
Difference between copy constructor and clone?
Can we achieve deep copy using serialization?
Why is deep copy safer?
Understanding this concept is very important for working with:
DTO objects
Hibernate entities
Microservices communication
Caching systems
Large enterprise applications
🎯 Final Summary
Shallow Copy → Copies object, but shares references
Deep Copy → Copies object and all nested objects
Shallow copy is faster but risky
Deep copy is safer but consumes more memory
Choosing between shallow and deep copy depends on your application requirements.
🚀 Learn Advanced Java with Real-Time Projects
Understanding cloning, object lifecycle, serialization, multithreading, and memory management is crucial for backend development.
If you want hands-on learning with industry-level implementation, check out:
🔥 AI powered Java Real Time Projects Online Training in Hyderabad
In this program, you will:
✔ Work on real-time enterprise projects
✔ Learn advanced Core Java concepts
✔ Master cloning, serialization & JVM internals
✔ Build Spring Boot & Microservices applications
✔ Gain AI-integrated backend development experience
✔ Prepare confidently for interviews
Strong fundamentals + real-time projects = Career success 🚀


Comments
Post a Comment