# What is the difference between copy() and deepcopy()?

**URL:** <https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918>\
**Category:** General Usage\
**Tags:** deepcopy\
**Created:** [May 25, 2017, 3:06am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918 "2017-05-25T03:06:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 25, 2017, 3:06am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/1 "2017-05-25T03:06:41Z")

</div>

```julia
julia> a = [1,2,3,4];
julia> b = a;
julia> a[1] = 4;
julia> b
4-element Array{Int64,1}:
 4
 2
 3
 4

```

This is the shallow copy AFAIK.

But at a glance, I couldn’t find clear differences between `copy()` and `deepcopy()`, although Docs says `copy()` creates a shallow copy of a collection.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 25, 2017, 4:44am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/2 "2017-05-25T04:44:39Z")

</div>

None of the operations above are copies, they are assignments. For understanding the difference between `copy` and `deepcopy`, consider

```julia
A1 = [[1]]
A2 = copy(A1)
A1[1] === A2[1] # true
A3 = deepcopy(A1)
A1[1] === A3[1] # false

```

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [May 25, 2017, 6:28am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/3 "2017-05-25T06:28:23Z")

</div>

These are related to copying mutable objects. I assume you know the difference between copying by value and copying by reference. Let ‘a’ be a mutable object of some type.

b = a copies ‘a’ by reference, so ‘b’ and ‘a’ refer to the same object. Therefore b.field1 = 2 makes a.field1 == 2 true.

b = copy(a) makes a new object of the same type as the object which ‘a’ refers to, and makes ‘b’ refer to it. So there are now 2 different objects, one for ‘a’ and one for ‘b’. Then for each ‘field’ of the new object, b.field = a.field is called. Therefore, if the field type is immutable it will be copied by value. If it is mutable, it will be copied by reference. Notice that this is a shallow copy, as b.field1.field11 = 2 still makes a.field1.field11 == 2 true, where field1 is a mutable object of some type.

b = deepcopy(a) keeps unwrapping any mutables inside of ‘a’ until it reaches all the immutables at all the levels, and copies all the data and structure of the old object to a new object. So ‘b’ and ‘a’ become completely independent, and changing one at any level does not change the other. For example b.field1.field11.field111 = 2 does not change a.field1.field11.field111. This however comes at the cost of using more memory when dealing with deep structures, e.g. a root node in a deep tree.

---

<div class="post-metadata">

**Author:** ![leejm516](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leejm516/32/8219_2.png) [@leejm516](https://discourse.julialang.org/u/leejm516)\
**Post date:** [May 25, 2017, 8:10am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/4 "2017-05-25T08:10:16Z")

</div>

Everything clear! Thank you.

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [January 22, 2020, 7:34pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/5 "2020-01-22T19:34:46Z")

</div>

Then  
when would you use b=a instead of b=copy(a) if having mutable fields?  
And  
when would you use b=copy(a) instead of b=deepcopy(a) if having mutable fields?  
Is it even needed the “copy” command?

I think will just be using arrays, tuples and dictionaries, not structs nor other complex things. I guess then it’s enough with copy.

---

<div class="post-metadata">

**Author:** ![Vahid\_Hosseinzadeh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vahid_hosseinzadeh/32/33293_2.png) [@Vahid\_Hosseinzadeh](https://discourse.julialang.org/u/Vahid_Hosseinzadeh)\
**Post date:** [September 5, 2022, 2:07pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/6 "2022-09-05T14:07:00Z")

</div>

So, I can not understand then, why in the case of String, it copies by reference,

```julia
a = ["hi"]
b = copy(a)
pointer(a) == pointer(b) #false of course
pointer(a[1]) == pointer(b[1]) #true! 

```

So it copies the string by reference while Strings are immutable in Julia.  
Can you make this compatible with your explanation?

I guess maybe the point is not that it is mutable or not. It might be whether it is a plain data type, meaning it is immutable and contains no references to other values. right?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [September 5, 2022, 2:51pm UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/7 "2022-09-05T14:51:58Z")

</div>

This is not related to whether strings are mutable or immutable, but their size in bits - since strings have variable size, they cannot be stored inline in arrays.

Either way, comparing strings by their pointers may or may not give you the result you’re looking for - we’re not in C. Egality checking is best done with `===`.

---

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 1, 2024, 5:39am UTC](https://discourse.julialang.org/t/what-is-the-difference-between-copy-and-deepcopy/3918/8 "2024-01-01T05:39:44Z")

</div>

why do you take

```julia
A1 = [[1]]

```

Why not

```julia
A1 = [1]

```
