The life time of loc..

Download Report

Transcript The life time of loc..

The life time of local variables (in
a method)
Local variables
• Local variable:
• A local variable is used to store information that is
relevant for the duration of the execution of one
method
Local variables (cont.)
• How to the define a local variable:
• A local variable is defined inside the body of a method
(I.e., a local variable is defined between the opening and
closing braces of a method)
Local variables (cont.)
• Example:
public class MyProgram
{
public static void main(String[] args)
{ // Body of method "main"
double r; // *** Local variable !!!
r = MyProgran.min( 1.0, 4.0 );
System.out.println(r);
r = MyProgram.min( 3.7, -2.9 );
System.out.println(r);
r = MyProgram.min( -9.9, 3.8 );
System.out.println(r);
}
}
Local variables (cont.)
public class ToolBox
{
public static double min ( double a, double b )
{ // Body of method "min"
double m = 0; // *** Local variable !!!
if ( a < b )
{
m = a; // a is the smaller value
}
else
{
m = b; // b is the smaller value
}
return(m);
}
}
Local variables (cont.)
Next, we study the life time and the scoping rules for local
variables
The life time of variables
• Kind of like animals, variables in a computer program has
a life time:
• A variable is created ("born") at a certain moment
• It exists for some time while the computer program
is executing....
• And then the variable ceases to exist
The life time of variables (cont.)
• Previously discussed:
• A variable in a running computer program is in fact a
memory cell (See:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/0
4/float.html#variable )
• Creating a variable = reserve memory space for a
variable (See:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/0
4/float.html#create-variable)
The life time of variables (cont.)
• Computer Science Jargon:
• Destroying a variable = reclaim the reserved memory
space occupied by a variable
When the reserved memory space taken up by a
variable is reclaimed, that variable will cease to exist
The life time of variables (cont.)
• Definition: the life time of a variable:
• Life time of a variable = the duration between the
moment when a variable is created and the moment when
a variable is destroyed
The life time of a local variable
• Moment of creation (of a local variable):
• A local variable is created at the moment that the
program execution reaches the definition of the local
variable
The life time of a local variable
• Moment of destruction (of a local variable):
• A local variable is destroyed at the moment that the
program execution reaches the end of the method in
which the local variable is defined
Example: how local variables are created
and destroyed
• Consider the
following program:
Example: how local variables are created
and destroyed (cont.)
• When the program starts to run, the RAM memory
contains only the program instructions:
Example: how local variables are created
and destroyed (cont.)
• The program starts to run with the main method.
The current program location is given by a green arrow:
Example: how local variables are created
and destroyed (cont.)
Example: how local variables are created
and destroyed (cont.)
• Note:
• There are 2 dark green arrows in the figure.
• The arrow in the left figure points to the current program
location in the human readable Java program
The Java program must be compiled (translated) into
machine instruction to be executed.
• The Java program is given to help you follow
what the computer will do
Example: how local variables are created
and destroyed (cont.)
• The arrow in the right figure points to the current program
location in the machine instructions program
• The machine instruction program is the actual
program that is executed by the computer !!!
Example: how local variables are created
and destroyed (cont.)
• Here is how the local variables are created and destroyed:
• When the computer encounters the definition of the
local variable r, it creates it in RAM (in other words:
reserve memory to hold the value)
Example: how local variables are created
and destroyed (cont.)
Example: how local variables are created
and destroyed (cont.)
• When the computer executes the method call, the program
execution is transferred , to the min method:
Example: how local variables are created
and destroyed (cont.)
• When the computer encounters the definition of the local
variable m, it creates it in RAM (in other words: reserve
memory to hold the value)
Example: how local variables are created
and destroyed (cont.)
• The program execution continues to compute the minimum of
the input values and store it in the local variable m:
Example: how local variables are created
and destroyed (cont.)
• The return(m) statement will save the value of variable m in a
return location:
Example: how local variables are created
and destroyed (cont.)
The return location is typically a general purpose register inside
the CPU (Central Processing Unit)
(CPU, see:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/01/intro
-computer2.html#CPU )
Example: how local variables are created
and destroyed (cont.)
• When program execution reaches the end of the function, the
local variable m is destroyed:
Example: how local variables are created
and destroyed (cont.)
Furthermore, the program execution will be
transferred back to the location of the method
call.
Example: how local variables are created
and destroyed (cont.)
• When program execution continues the location of the method
call, the assignment statement will stored the value in the
return location to the local variable r:
Example: how local variables are created
and destroyed (cont.)
• And when the program reaches the end of the main method,
the local variable r will also be destroyed:
Example: how local variables are created
and destroyed (cont.)
• Conclusion:
• A local variable only exists between:
• The definition of the local variable
• The end of the method where the local
variable is defined
Example: how local variables are created
and destroyed (cont.)
Example 1:
• The local variable m only exists within this green
area:
Example: how local variables are created
and destroyed (cont.)
Example 2:
• The local variable r only exists within this green
area: