Variables Point to Values

Learn to Program, Third Edition — by Chris Pine (21 / 116)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Chapter 3 Variables and Assignment | TOC | Progress Checkpoint 👉

As you’ve seen, a variable can point to any kind of object. What happens if you try to point a variable to another variable? Let’s see:

​<= 8
​ 8

​ luck
​ 8

Notice that on line 2, var2 points to var1, which is pointing to 8. Then on line 8, we reassign var1 so that it points to “luck”. So why is puts printing 8 for var2 instead of “luck”? Good question.

The issue here is that var2 was never really pointing to var1. Variables can’t point to other variables, so var2 was actually pointing to 8 the whole time.

var2 was actually pointing to 8 the whole time

👈 Chapter 3 Variables and Assignment | TOC | Progress Checkpoint 👉

Learn to Program, Third Edition by Chris Pine can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.