Class-First Coding
© 23 Oct 2013 Luther Tychonievich
Licensed under Creative Commons: CC BY-NC-ND 3.0
other posts

Learning to Program

Lesson 3: Memory, Variables, Assignment, and Return.

 

Conceptual Overview

In the first lesson we introduced the idea of a class having fields and methods and the difference between a class of objects and a particular object that is an instance of that class. In the second lesson we talked more about how methods are defined and mentioned that there are rules about how method bodies are defined. This lesson will discuss how we make objects from a class and some of the things we can put in a method body.

Inside of a computer is a lot of memory. From the computer’s perspective, this is all one big scratch pad on which a program can write anything it wants. Most programming languages divide it into several distinct regions, including the stack and the heap.

The stack represents the ideas of a single worker invoking methods. When I am making a pie I need to remember where I am in the pie recipe and which bowl currently has what inside it. If you are also making a pie at the same time with the same recipe you’ll have your own stack: your own memory of where you are and where you put the things you have handled so far. It is called a “‍stack‍” because of how we react when the pie recipe says “‍make a pie crust (see page 93)‍”: we don’t lose the memory we have so far but we “‍stack‍” on top of it a clean sheet for doing pie crust. When the pie crust calls for butter we know it’s not the same butter that the pie recipe called for because that old butter has been covered up by the new memory reference, the new “‍stack frame‍” in programing parlance. Once the pie crust is done it “‍returns‍” the crust to us and the throw away the pie crust memory, revealing the old pie memory that was underneath it all along. That act of tossing out the memory for completed methods is called “‍popping the stack.‍”

The heap represents the world at large, all the objects that exist at a given time. My memory of which lump of butter is part of my pie recipe is in the stack, but the lump itself, being an object, is in the heap. Just about everything goes on the heap, except for values of primitive types Some languages, such as C++ and D, have mechanism for putting some non-primitives on the stack but this is the exception, not the rule. . There isn’t some “‍3‍” object out there, we each have our own copy of “‍3‍” in our heads.

When I define a class whose fields’ types are themselves classes, such as a Car with a field whose type is Wheel, the objects actually store the same thing the stack would: where to find their parts and pieces elsewhere on the heap. If I poke inside a Car object I’ll find not a Wheel object but rather a reference to which Wheel object on the stack this car is using.

Variables and Assignment

Fields and parameters are both examples of variables. A variable is a named thing, the meaning of which can vary over time. This meaning is different from the meaning in mathematics: in a mathematical system of equations, each variable has one and only one value (x can’t be 3 in some equations and 5 in others). In most programming languages, we can change and re-change each variable’s value.

Confusingly, in the vast majority of languages the code for changing a variable is variableName = newValue; In any other context = means “‍is equal to‍” and is called the “‍equals sign‍”. In programming = means “‍will hereafter have the value‍” and is called the “‍assignment operator‍”. It is perfectly OK in programming to say something like x = x + 1; this means “‍change x so its new value is its old value plus one.‍”

The other thing to know about variables is that you can make as many as you want inside a method body. The syntax for this is just like it was to make a field: TypeName variableName.

return, this, and .

In introducing methods in the previous lesson I wrote descriptions like “‍set this Car’s speed to be newSpeed‍”. We just learned that “‍set x to be y‍” is written x = y;; to finish writing this method we need to know how to write “‍this Car’s speed‍”.

Each method we write in class Car has access to “‍this Car‍”, each method in class Fraction has access to “‍this Fraction‍”, etc. To reduce extraneous typing, the special variable this represents “‍this X‍” in the methods of class X.

Most languages write “‍’s‍” as a single period, .. This period is called the “‍membership operator‍”; we’ve already seen it when discussing method invocation last lesson.

Putting these ideas together, the full changeSpeed method for Car is: void changeSpeed(double newSpeed) {
this.speed = newSpeed;
}

A related tool tool is the return statement. The code return x; (1) stops executing the current method and (2) returns the value of x as the result of the method invocation. We might use this as, e.g., double speedInMPS() {
double multiplier = 0.44704;
return this.speed * multiplier;
}
Note that multiplication is written * in most programming languages; more on this in a latter lesson.




Looking for comments…



Loading user comment form…