# Confused by different behavior of assignments of Arrays and Numbers

**URL:** <https://discourse.julialang.org/t/confused-by-different-behavior-of-assignments-of-arrays-and-numbers/50182>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [November 15, 2020, 12:28pm UTC](https://discourse.julialang.org/t/confused-by-different-behavior-of-assignments-of-arrays-and-numbers/50182 "2020-11-15T12:28:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aachener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aachener/32/18112_2.png) [@aachener](https://discourse.julialang.org/u/aachener)\
**Post date:** [November 15, 2020, 12:28pm UTC](https://discourse.julialang.org/t/confused-by-different-behavior-of-assignments-of-arrays-and-numbers/50182/1 "2020-11-15T12:28:37Z")

</div>

Hello, everyone,

This is rudimentary, but I’am confused about Julia’s different “=” behaviors for Numbers and Arrays.

To my understanding, when we assign a variable to an object, we set the variable referred to the memory location of the object. Then if we assign this variable to another variable, behavior changes between numbers and arrays. Like this:

```julia
julia> x = 10
10
julia> x1 = x
10
julia> x1 === x
true
julia> x1 = 5
5
julia> x
10
julia> x1 === x
false

```

Which means for numbers, x1 initially is exactly the same with x before we assign a new object to it.

But for Arrays, this is a different story, like this:

```julia
julia> y = [10]
1-element Array{Int64,1}:
 10
julia> y1 = y
1-element Array{Int64,1}:
 10
julia> y1 === y
true
julia> y1[1] = 3
3
julia> y
1-element Array{Int64,1}:
 3
julia> y1 === y
true

```

Which means y1 and y always point to the same memory location. But we can use copy() and deepcopy() to separate y1 and y.

I know this is rudimentary, but I’m really curious about why it is designed this way? Why don’t we align these two different behaviours? Can anyone explain to me the mechanism underground?

Thanks!

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [November 15, 2020, 12:48pm UTC](https://discourse.julialang.org/t/confused-by-different-behavior-of-assignments-of-arrays-and-numbers/50182/2 "2020-11-15T12:48:46Z")

</div>

This is a common source of confusion for beginners (myself included). You can find related threads about it. One nice answer is this one:

> [@Assignment and mutation](https://discourse.julialang.org/t/assignment-and-mutation/19119/4):
>
> The simplest version of this I can come up with is this: Assignment changes which object a name refers to: x = ex causes the name x to refer to the value resulting from the evaluation of the expression ex. Assignment never changes the values of any objects. Mutation changes the value of an object: x[i] = ex and x.f = ex both mutate the object referred to by x changing a value at index or a property with a name, respectively. Mutation never changes what objects any names in any scope refer …

---

<div class="post-metadata">

**Author:** ![aachener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aachener/32/18112_2.png) [@aachener](https://discourse.julialang.org/u/aachener)\
**Post date:** [November 15, 2020, 1:27pm UTC](https://discourse.julialang.org/t/confused-by-different-behavior-of-assignments-of-arrays-and-numbers/50182/3 "2020-11-15T13:27:24Z")

</div>

thank you for the link, very clearly explained!
