References

In this lab, you will study reference variables.

Storage of Variables

Because variables are used so frequently, they need to be in a place where they can be quickly accessed. The stack provides this place. Each local variable, whether it is a primitive data type or a reference variable, is stored on the stack. When you use a variable's name, the computer quickly locates that variable in memory and retrieves its value.

Physically, the stack is nothing more than a big block of memory. The system handles all the details of managing its size so you don't have to worry about it.

Variable Passing

When you call a method, that method is given a portion of memory at the top of the stack to store its local variables and parameters. Because each method uses its own portion of the stack, we know that one method cannot modify the local variables of another methods.

When a method returns (or finishes running), the portion of memory on top of the stack used by that method is freed by the system. In other words, that method's variables no longer exist in memory. The following Stack N' Heap demonstrates this:


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
{
int myVar = 5;
dosomething ( );
System.out.println( myVar );
}

public void dosomething ( ) {
int myVar = 18;
System.out.println( myVar );
}
Stack
Heap
Temporary

Because methods do not share memory, the only way methods can share information is via return values and parameters. A method can return information to the method that called it using a return value. A method can send information to the method it is calling by giving values to that method's parameters. The following Stack N' Heap demonstrates the use of return values.


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
{
int myVar = 5;
myVar = dosomething2 ( myVar );
System.out.println( myVar );
}

public int dosomething2 ( int temp ) {
return ( temp + 15 );
}
Stack
Heap
Temporary
Storage of Objects

All objects are stored on the heap. The system cannot find an object once it is stored on the heap unless it knows its address; an object addresses are stored in reference variables. Although objects that are not referenced by a reference variable are useless, they still take up space in memory. To reclaim this space, a procedure called garbage collection is run. This process scans the heap to find objects that can be removed. The system handles this automatically; you needn't worry about it.

Passing Variables

As mentioned above, a method can send information to a called method by giving values to that method's parameters. A method's parameters are declared in the parenthesis immediately following the method's name. The syntax for declaring a parameter is very simple, it is identical to declaring a variable. For instance: the method header public void dosomething (int param1, String param2) begin a method named dosomething that has two parameters: an integer named param1, and a String reference variable named param2. In order to call this method, you must provide an initial value for each of these parameters, such as dosomething(5, "This is a test");. (Note that a comma separates the two expressions supplied for the parameters.) The values "passed" to the method when you call it become the initial values of that method's parameters.

Passing Primitive Data Types

When you call a method you must specify the initial values of its parameters; these values can be changed as the method runs. Using the assignment operator, you can change the value stored in a parameter, but this will have no effect on the variable stored in the calling method:


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
{
int myVar = 5;
dosomething3 ( myVar );
}

public void dosomething3 ( int temp ) {
temp = temp + 15;
}
Stack
Heap
Temporary

Passing Reference Variables

Remember that reference variables store addresses. When a parameter is a reference variable, it must be given an object's address as its initial value. You can use the assignment operator to change the address stored in a reference parameter. You can also use the address stored in the reference parameter to change the instance variables of the object to which it refers. These changes will exist after the method stops running. The following Stack N' Heap illustrates this:


Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primative variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
Code
{
intHolder myVar = new intHolder ( 5 );
dosomething4 ( myVar );
System.out.println( myVar.getInt() );
}

public void dosomething4 ( intHolder temp ) {
temp.setInt ( 20 );
}
Stack
Heap
Temporary
References
To Get Started ...

Download the jar file: References.jar to your csci/201 directory, unjar the file (Help), create a new NetBeans project called References, and mount the References directory created when you unjarred the archive file (Help). Your directory structure should look something like the picture below:
After the jar

Reference Variables

The file DeuxTons.java defines a simple class that represents an object with two colors; one, a "top" color and, the another, a "bottom" color. The file References.java contains a main method that instantiates two DeuxTons objects.

The main method makes eight calls of the static Click method of the class TwoToneDisplay, four times for each DeuxTons object. The Click method takes a "picture" of a DeuxTons object. It displays the picture inside a small frame with labels similar to 3'th picture. That label indicates the picture's number within the "roll".

The picture contains two circles. The top circle has the "top" color of the DeuxTons object. The bottom circle has the "bottom" circle. Between the two colored circles, you will see a line proclaiming something like I'm 342589. That number is an object ID, obtained by calling the hashCode method. This ID is guaranteed to be unique for every Java run-time object.

Go ahead and run Reference's main method. You should see the eight Click's. They will probably fill your screen. If you click the X on any picture, all will disappear.
Before modification

Your Assignment ...

Within the main method of References there are comments that describe modifications you are to make to DeuxTons objects or to references to DeuxTons objects during this lab. After each modification, main calls the Click twice to display the result of your modification. Your job is to add code to the main method so that it performs the modifications described in the comments in References.java.

There are three modifications that you that you must make. Some of these modifications will require that you call the getTop, getBottom, setTop, and setBottom methods of DeuxTons. You should look at the code within DeuxTons.java to learn more about these methods; however, the only file you should modify to complete this lab is References.java.

Instructor Checkoff ...

Show your lab instructor your complete code with all modifications. The ultimate display should resemble the following images. Pay careful attention to the object ID's displayed in the middle of each picture.
After modification