Concept Flow - Identity comparison (===)
Create Object A
Create Object B
Compare A === B?
Yes→Same Object
No
Different Objects
This flow shows creating two objects and then checking if they are exactly the same object in memory using ===.
class Person {} let p1 = Person() let p2 = p1 let p3 = Person() print(p1 === p2) print(p1 === p3)
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Create p1 as new Person() | p1 points to Object1 | p1 -> Object1 |
| 2 | Assign p2 = p1 | p2 points to same Object1 as p1 | p2 -> Object1 |
| 3 | Create p3 as new Person() | p3 points to Object2 | p3 -> Object2 |
| 4 | Evaluate p1 === p2 | Are p1 and p2 same object? | true |
| 5 | Evaluate p1 === p3 | Are p1 and p3 same object? | false |
| Variable | Start | After Step 1 | After Step 2 | After Step 3 | Final |
|---|---|---|---|---|---|
| p1 | nil | Object1 | Object1 | Object1 | Object1 |
| p2 | nil | nil | Object1 | Object1 | Object1 |
| p3 | nil | nil | nil | Object2 | Object2 |
Identity comparison (===) checks if two variables point to the exact same object in memory. Use === only with class instances (reference types). If two variables refer to the same object, === returns true. If they refer to different objects, even if identical in content, === returns false. Useful to check object identity, not equality of content.