Pointers and References

Pointers and References are variables that contain the "address" in memory of a piece of data, rather than the actual data. For a real world example, consider your name in the class grade book... Is this name the actual you (the human body)? NO. Does your name provide information that allows the professor to "refer to" (or reference) you? YES! Can there be multiple names that all refer to you? Of course. The teacher can call you Mr. Doe, your friends can call you, Homer, and your mom can call you Sparkle. These names are all "references" to the same data (you).

Pointers and References

Pointers and References: Analogy

Imagine a card catalog in a library. When you know the name of a book, you don't know WHERE in the library it is. To find the book, first you go to the card catalog, and the card catalog POINTS you to the book (or in other words is a reference to the book).

Alternatively, you may go to the computer and search for the book by its name or by its author. Either way, you will get a "reference" to where the book is in the library. Now you have two references, but they both "point to" the same thing.

This is how some data is stored in a computer program. Instead of keeping the data directly in the memory bucket, the memory bucket contains a "pointer" to the actual location of the data.

This is useful when we don't want to "copy" the data into different buckets, but instead have everyone "reference" the original data (but via different names).

Modifying a value via a Reference

Consider the following code.

ActionScript (and Java)

In Actionscript (and Java) there is no special notation for references. EVERY variable that is not a base type (e.g., Number, int, Boolean) is a Reference!

With a "reference" variable, when we "assign" the contents from one variable to another, we don't duplicate the data, we "share" the data. Below you will see code the shares the single object Student: "Jim" between to buckets. Any change to one bucket affects the other.

        
              var student : Student = new Student("Jim");   // the "student" variable contains a reference to "Jim"
              var other   : Student = student;              // the "other" variable also contains a reference to "Jim" 

              student.name = "James"; // rename the student

              trace("The students name is: " + student.name ); // prints James

              trace("The other name is: " + student.name ); // prints James as well

              other.name = "Joe";

              trace("The students name is: " + student.name ); // Prints Joe!
        
      

Example Two: Houses

        
              var house   : House = new House();
              var other   : House = other;  // there is only one house, but two names for it

              house.visible = false;    // the house would not be shown

              other.visible = true;     // the house is now shown

              house.x = 100; // house.x and other.x refer to the same thing, where
              other.x = 200; // the "single" house is on the stage.
              
        
      

Here is an example of how you should look at things:

ladder diagram of reference variable

Notice how we draw arrows to represent the sharing of the object.

With base types, when we "assign" a value from one variable to another, we copy the data. For example:

        
              var grade     : Number = 97;
              var my_grade  : Number = my_grade; // COPIES the value

              trace("My grade is:   " + my_grade ); // prints 97
              trace("Your grade is: " + grade );    // prints 97

              my_grade = 100; // changes value in "my_grade" bucket,
                              // but does not change the value in the "grade" bucket
                              
              trace("Your grade is now: " + grade ); // prints 97!
        
      

After this code is executed, the values would be as shown in the picture.


Passing a Parameter via Reference

Parameter passing takes one of two forms:

In General, each programming language has its own rules and defaults for whether to use pass by value, or pass by reference.

ActionScript

All non-primitives are passed by reference. All primitives are passed by value (copied).

Remember: the following types are considered primitives in AS3: Boolean, int, uint, Number, and String.

Java

All non-primitives are passed by reference. All primitives are passed by value (copied).

Remember: the following types are considered primitives in Java: int, long, float, double, boolean, char. Beware: Strings and Numbers also act as primitives when being assigned or passed.


Why references? Why not just make copies every time we write variable_x = variable_y??

The power of references is that it allows different sections of your program to refer to the same piece of information, but to do so with a different name! The the code for a "House", inside the class House file, can refer to the object as "this". In code outside of the House.class file, we can refer to the house object by its variable name, say: house_1;

Well then, how can I get a "Copy" of an Object.

To create a new house, we can simply use the code:

        
          var x: House = new House();
        
      

Thus building a complete new house. But what if you want to copy an existing house? This requires something like:

        
          var old_house : House = ....; // here is the original house


          // here we build a new house, but then we will copy the values
          // from the old house into it...
          var new_house : House = new House();

          // ...such as:
          new_house.x = old_house.x;
          new_house.y = old_house.y;
          new_house.visible = old_house.visible;
          // ETC, ETC, ETC (could be a lot of work)

          // Some times object recipes expect you to want to "copy" an object
          // and they provide the wonderful "clone" function.  The clone
          // function does all of those previous assignments for you in one line,
          // thus:

          var new_house : House = old_house.clone();
          
        
      

Back to Topics List