If x = “hi”, x is not “hi”

Arun Kavishwar
3 min readOct 10, 2021

This is a fundamental concept for tech interviews.

Are the variables you define really what they seem?

You already read the title. This is a common misconception that is made by a lot of programmers, and can come in handy when understanding a number of topics. When designing programming languages, this concept has massive implications. More commonly, when working with mutable data structures (as commonly done in software engineering interviews), it is necessary to understand why x is not “hi”, or even anything remotely equal to “hi”.

What?

x = “hi”

Innocent enough, right? When you evaluate the expression x, you will get the value “hi”. So why am I telling you differently? You see, the string is actually formed and stored in memory, at some address in the current environment. For example, let’s say the characters “h” and “i” were bound together at address @1000. Then, x stores — not “hi” as the syntax may imply — but rather, the address @1000. So,

x = @1000

Funnily enough, when you print (x), it actually should show you @1000. However, the developers of your favorite programming language have probably decided that the memory address is not useful to you, and actually do the extra step of jumping to that memory address, finding the value there, and printing it out for you.

Who cares?

--

--

Arun Kavishwar

Brown University CS, SWE Intern, and Mock Trial enthusiast