# Why is collect(1:3) === \[1, 2, 3\] false?

**URL:** https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915
**Category:** General Usage
**Tags:** question
**Created:** [August 24, 2021, 3:12pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915 "2021-08-24T15:12:01Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)
#### Post date: [August 24, 2021, 3:12pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/1 "2021-08-24T15:12:01Z")

</div>

Both sides of the comparison have the same values and the same type. I must not understand === correctly.

---

<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: [August 24, 2021, 3:14pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/2 "2021-08-24T15:14:26Z")

</div>

`===` compares _object identity_, while `==` compares _object equality_. The former checks whether two objects are the exact same object (more formally, whether they are indistinguishable by any computer program representable in julia), whereas the latter “only” checks whether they reasonably behave the same (but are still distinguishable).

In this case, the two arrays have the same contents, but are two distinct objects.

---

<div class="post-metadata">

### Author: ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)
#### Post date: [August 24, 2021, 3:31pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/3 "2021-08-24T15:31:47Z")

</div>

I’m thinking the reason they are separate objects is because they are mutable; is that right? If I assign the left- and right-hand-sides to different variables, I can mutate them separately.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 24, 2021, 3:38pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/4 "2021-08-24T15:38:30Z")

</div>

```julia
[1,2,3] === [1,2,3]
false # ???

```

---

<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: [August 24, 2021, 3:41pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/5 "2021-08-24T15:41:22Z")

</div>

> [@LeePhillips](#):
>
> I’m thinking the reason they are separate objects is because they are mutable; is that right? If I assign the left- and right-hand-sides to different variables, I can mutate them separately.

More or less, yes.

> [@rafael.guerra](#):
>
> ```julia
> [1,2,3] === [1,2,3]
> false # ???
> 
> ```

These again construct two distinct objects that happen to contain the same elements.

---

<div class="post-metadata">

### Author: ![LeePhillips](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leephillips/32/205514_2.png) [@LeePhillips](https://discourse.julialang.org/u/LeePhillips)
#### Post date: [August 24, 2021, 3:48pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/6 "2021-08-24T15:48:57Z")

</div>

I understand. The two things have different memory locations, so you could write a program to distinguish them. Whereas with simple values, like a = 1.0 and b = 1.0, a and b are identical; they are immutable and have no fixed address.

---

<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: [August 24, 2021, 3:50pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/7 "2021-08-24T15:50:10Z")

</div>

Precisely - this also extends to immutable custom structs:

```julia
julia> struct ImmutableExample                    
           a::Int                                 
       end                                        
                                                  
julia> ImmutableExample(4) === ImmutableExample(4)
true                                              
                                                  
julia> mutable struct MutableExample              
           a::Int                                 
       end                                        
                                                  
julia> MutableExample(4) === MutableExample(4)    
false                                             

```

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 24, 2021, 4:15pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/8 "2021-08-24T16:15:52Z")

</div>

just to demonstrate what you said with a related example, one way to think about `===` is to think about:

```julia
julia> a = [1,2,3]; a2 = a; b = [1,2,3];

julia> a===a2
true

julia> a == a2 == b
true

julia> push!(a2, 0);

julia> a === a2, a2==b
(true, false)

```

---

<div class="post-metadata">

### Author: ![genkuroki](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/genkuroki/32/18030_2.png) [@genkuroki](https://discourse.julialang.org/u/genkuroki)
#### Post date: [August 24, 2021, 4:38pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/9 "2021-08-24T16:38:38Z")

</div>

Let’s consider an array as a wallet. 😝

Executing [1, 5, 10] will create a wallet with $1, $5, and $10 bills inside. 🤑

Executing [1, 5, 10] again will create another wallet of the same type with the same contents. 🤑

Comparing them with `==` will compare if the contents are equal and the result will be true. 💵

If they are compared with `===`, they are wallets of the same type with same contents but different, so the result will be false. 🥳

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 24, 2021, 5:31pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/10 "2021-08-24T17:31:54Z")

</div>

Tuples are different kind of wallets:

```julia
(1,2,3) === (1,2,3) # true

```

_NB: less money is printed with them 💵_

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [August 24, 2021, 5:50pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/11 "2021-08-24T17:50:32Z")

</div>

playing along: are they wallets if you can’t add or take money out of them?

---

<div class="post-metadata">

### Author: ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)
#### Post date: [August 24, 2021, 5:55pm UTC](https://discourse.julialang.org/t/why-is-collect-1-3-1-2-3-false/66915/12 "2021-08-24T17:55:48Z")

</div>

```julia
?===

```

gives

```julia
help?> ===
search: === == !==

  ===(x,y) -> Bool
  ≡(x,y) -> Bool

  Determine whether x and y are identical, in the sense that no program could distinguish them. First the types of x and y are
  compared. If those are identical, mutable objects are compared by address in memory and immutable objects (such as numbers) are
  compared by contents at the bit level. 

```

So, given that the comparison is of two vectors, which are mutable, the comparison involves checking the address in memory, which will be different.
