DEV Community

NKO
NKO

Posted on

Day 7: Reference Wilderness

Another day, another metaphorical rabbit hole I dove down in this code labyrinth. Today's target: primitive types vs. reference types. Turns out, just like people, variables can be deceiving. You think you know someone (a variable, in this case), and BAM! They're pointing you towards something entirely different.

Primitive types are the loners, the ones who like to store their own damn data. We created a variable for the color "orange," and that's exactly what it held - the value "orange" itself. But then we went and made another variable, "fruit," and assigned it the value from "color." Here's the twist: changing the fruit didn't affect the color one bit. That's because they're each their own little island, no connection between them. Makes sense in a way, but kinda feels isolating for a variable, you know?

Then there are the arrays, the social butterflies of the variable world. Assigning an array to a variable isn't like shaking hands; it's more like handing over a map. The variable itself doesn't hold the actual array data, just a fancy pointer, a little "you-go-this-way" sign leading to where the real array lives in memory. This is why we can push new values to an array assigned with const - we're not changing the reference itself, just adding furniture to the house the pointer points to. Confusing, right? Feels like a constant shell game, gotta keep my eye on the pointer, not the variable itself.

And to top it all off, we can shove arrays inside other arrays. Like, inception for data storage. It's powerful, sure, but man, this is starting to look like a tangled mess of pointers and references. Gotta admit, the image doesn't exactly inspire confidence. Feels like I'm staring into the abyss of ever-increasing complexity.

Today was a day of cognitive overload. But hey, at least I'm learning, right? This whole reference type thing feels like a cheat code compared to primitive types, but it also comes with its own set of rules. Gotta keep that map metaphor in mind, and maybe draw some diagrams to visualize this whole mess. One step closer to understanding this digital jungle, I guess. Just gotta make sure I don't get lost in the reference wilderness.

Top comments (0)