# CS 200 Lecture 5 (from FP): Variables and Values
(by Kathi Fisler)
## Motivating Problem
What happens if the value of an attribute of an animal (like what a boa eats) changes in the real world? How can we get our code to maintain these changes for us?
## Conceptual Study Questions
- What programming pattern do we need to use in code if we try to update a field by creating a new object?
- How do changes in field values show up in memory diagrams?
# Table of Contents
[TOC]
# Capturing Changes in Data
Assume we have the following zoo with two animals:
```java=
Boa slimBoa = new Boa("Slim", 5, "lettuce");
Dillo hugeDeadDillo = new Dillo(60, true);
Zoo myZoo = new Zoo(slimBoa, hugeDeadDillo);
```
The zookeepers are worried that Slim isn't getting enough protein. They want to change Slim's diet to `"tofu"` instead of `"lettuce"`.
In functional programming, we would create a new Boa with this information:
```javascript=
new Boa("Slim", 5, "tofu")
```
But what do we ***do*** with this new `Boa`? Ideally, the zookeepers would want to see the new diet reflected in the `myZoo` object. In other words, if they print out `animal1` in `myZoo`, they want to see that `"Slim"` eats `"tofu"`. Our job is to make that happen.
Here's an idea -- let's just reassign the name `slimBoa` to a new Boa with the new details about Slim
```java=
// the original code
Boa slimBoa = new Boa("Slim", 5, "lettuce");
Dillo hugeDeadDillo = new Dillo(60, true);
Zoo myZoo = new Zoo(slimBoa, hugeDeadDillo);
// some time passes
Boa slimBoa = new Boa("Slim", 5, "tofu");
```
If we print out `slimBoa`, what will we see? What if we print 'animal1` from `myZoo`, what will we see?
```java
System.out.println(slimBoa):
// prints Boa("Slim", 5, "tofu")
System.out.println(myZoo.animal1):
// prints Boa("Slim", 5, "lettuce")
```
Wait -- what happened? We get different information about Slim depending on whether we go through the variable name or the zoo. That doesn't seem right.
To understand what happened here, it helps to draw the memory diagram. After the original code, we have the following diagram:

Now, when we make another `Boa` object for Slim and make `slimBoa` refer to that new object, our diagram looks as follows:

The diagram (which simply tracks how Java works -- it isn't something we can control) shows what happened. Changing the object that `slimBoa` refers to does not change any data that were previously constructed through that name.
Put differently, **when we use `=`, we change the environment, but we do not change the heap**. If we want a change to `slimBoa` to reflect anywhere that *object* is stored, we have to figure out how to change the contents of the heap.
## Changing object contents on the heap
Here instead is a line of code that *does* change the heap contents:
```java
slimBoa.eats = "tofu"
```
When Java evaluates a field reference, it first finds the object in the heap (via `slimBoa` from the environment), then finds the `eats` field within the object, then replaces the value within the object itself. In other words, the above line of code causes the following change to the original diagram:

The takeaway from this example is **when we use `object.field =`, we change the contents stored in that object in the heap**. The environment, however, is not changed.
## Understanding updates
The differences between updating variables and updating fields can be subtle. What we're discussing here applies in all mainstream programming languages (Java, Python, Pyret, Racket, Javascript, ... and more!). Here are rules of thumb:
- If you want a change to be temporary, make a new object and connect it to a new name
- If you want a change to be visible to future uses of the data, but not past uses of the data, make a new object and connect it to a variable name
- If you want a change to be permanent, change the value of the field within an existing object