CSCI 201
Introduction to Algorithm Design home | homework index | labs index |
FALL 2006 |
Let's start by taking a look at variables in Java.
Because variables are used so frequently, they need to be stored 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 block of memory. The system handles all the details of managing its size so you don't have to worry about it.
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, 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:
Code
{
int myVar = 5; dosomething ( ); System.out.println( myVar ); } public void dosomething ( ) { int myVar = 18; System.out.println( myVar ); } |
Stack
|
Heap
|
Temporary
|
Because methods in different classes do not share memory, the only way methods can share information is via parameters and return values. A calling method can send information to the called method by giving values to that method's parameters. A called method can return information to the calling method using a return value. The following Stack N' Heap demonstrates the use of return values.
Code
{
int myVar = 5; myVar = dosomething2 ( myVar ); System.out.println( myVar ); } public int dosomething2 ( int temp ) { return ( temp + 15 ); } |
Stack
|
Heap
|
Temporary
|
Variables can also refer to objects. The stack space for the variable only needs to contain the reference to the object. The objects themselves are stored on the heap, another area of memory that is much larger than the stack.. The system cannot find an object once it is stored on the heap unless it knows its address; object addresses are stored in reference variables.
Objects that cannot be accessed through a reference variable are useless. However, they may still take up may space in the heap. To reclaim this space, a procedure called garbage collection is periodicly run by the Java system. This process scans the stack and heap to find objects that can be removed. The system handles this automatically; you needn't worry about it.
As mentioned above, a calling 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)
begins 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");
.
The
values passed to the called method become the initial
values of the called method's parameters.
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:
Code
{
int myVar = 5; dosomething3 ( myVar ); } public void dosomething3 ( int temp ) { temp = temp + 15; } |
Stack
|
Heap
|
Temporary
|
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; however, this does not change any variables used by the calling method. Now you can 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: