Deep and Shallow Copy in Java
Object copying in general means creating a new object but using another existing object’s data. Like its memory addresses/references.
Deep and Shallow Copy in Java
Object copying in general means creating a new object but using another existing object’s data. Like its memory addresses/references.
Shallow Copy
We create a new object but do not duplicate referenced objects, just their references. In the end, both objects point to the same location of memory.
- Both object refers to the same values. If changed in one, it will affect the other too.
- Fast and memory efficient.
Example:
class shallow_copy{
private int[] data;
public shallow_copy(int[] values){
this.data = values;
}
}
------
class use_shallow{
public static void main(String[] args){
int[] vals = {3, 7, 9};
shallow_copy e = new shallow_copy(vals);
vals[0] = 13;
//new array values will be 13, 7 and 9.
}
}
Deep Copy We create a new object but with separate copies of all referenced objects. To ensure independence from each other. Meaning, one change won’t affect the other.
- When cloning, all of the referenced objects need to be cloned one by one.
- Safer than shallow copy.
- Uses more memory space.
- Could be more complex.
Example:
class deep_copy{
private int[] data;
public deep_copy(int[] values){
data = new int[values.length];
for(int i = 0; i < values.lenght; i++)
data[i] = values[i];
}
}
---------
class use_deep{
public static void main(String[] args){
int[] vals = {3, 7, 9};
deep_copy e = new deep_copy(vals);
vals[0] = 13;
//values in data still will be 3, 7 and 9. Because they're independent.
}
}
Another Type of Copy: Lazy Copy (Copy-on-Write)
This one is a hybrid copy of shallow and deep. For efficiency, we use both. Shallow copy is used for creation while deep copy is used when a write operation is needed.
Reference count is used to see how many users is using the list at the moment.
Example:
class SharedData {
int[] data;
int refCount = 1;
SharedData(int[] values) { this.data = values; }
SharedData copy() { return new SharedData(this.data.clone()); }
}
class LazyCopy {
private SharedData shared;
LazyCopy(int[] values) { shared = new SharedData(values); }
LazyCopy(LazyCopy other) {
this.shared = other.shared;
this.shared.refCount++;
}
void setValue(int index, int value) {
if (shared.refCount > 1) {
shared.refCount--; // I'm leaving the group
shared = shared.copy(); // I'm making my own private copy
}
shared.data[index] = value;
}
}
- Let’s say I’m using the list, reading it currently. No changes yet. This makes reference count to 1.
- And you come to read the list. This makes reference count rise to 2. Meaning there is 2 users who are using the list at the moment.
- Then I want to make some changes on the list. Lazy copy checks reference count and see it’s more than 1. And copies the original list for another memory address. And reference counts drop by one.
- I make my modifications on the new address. But you still see the old original data and do not see my modifications on it.
- Then you leave the list, not using it anymore. Reference count is now zero. No one is using the old list memory address. It gets deleted. Remember, I’m on the new address now. I’m not using it neither.
- When another user comes or you come back to the list, you’ll now be seeing the list on the new address with my modifications.
- This makes using copying/cloning both memory and time efficient because when you don’t need to make some many write operations, this is probably the best option you should use.
— — — — — — — — — — — — — — — —
메타데이터
- post_id
- 6edbe3e56f19
- slug
- deep-and-shallow-copy-in-java-6edbe3e56f19
- url
- https://medium.com/@eceaslan1001/deep-and-shallow-copy-in-java-6edbe3e56f19
- canonical_url
- https://medium.com/@eceaslan1001/deep-and-shallow-copy-in-java-6edbe3e56f19
- author_url
- https://medium.com/@eceaslan1001
- status
- ok
- fetched_at
- 2026-07-28 07:50:17