the life time of local variables (in a method). local variables local variable: a local variable is...

32
The life time of local variables (in a method)

Upload: herbert-underwood

Post on 17-Jan-2016

227 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 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

The life time of local variables (in a method)

Page 2: 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

Local variables

• Local variable:

• A local variable is used to store information that is relevant for the duration of the execution of one method

Page 3: 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

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)

Page 4: 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

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); } }

Page 5: 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

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); } }

Page 6: 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

Local variables (cont.)

Next, we study the life time and the scoping rules for local variables

Page 7: 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

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

Page 8: 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

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/04/float.html#variable )

• Creating a variable = reserve memory space for a variable (See: http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/float.html#create-variable)

Page 9: 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

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

Page 10: 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

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

Page 11: 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

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

Page 12: 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

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

Page 13: 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

Example: how local variables are created and destroyed

• Consider the following program:

Page 14: 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

Example: how local variables are created and destroyed (cont.)

• When the program starts to run, the RAM memory contains only the program instructions:

Page 15: 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

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:

Page 16: 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

Example: how local variables are created and destroyed (cont.)

Page 17: 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

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

Page 18: 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

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 !!!

Page 19: 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

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)

Page 20: 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

Example: how local variables are created and destroyed (cont.)

Page 21: 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

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:

Page 22: 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

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)

Page 23: 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

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:

Page 24: 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

Example: how local variables are created and destroyed (cont.)

• The return(m) statement will save the value of variable m in a return location:

Page 25: 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

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 )

Page 26: 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

Example: how local variables are created and destroyed (cont.)

• When program execution reaches the end of the function, the local variable m is destroyed:

Page 27: 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

Example: how local variables are created and destroyed (cont.)

Furthermore, the program execution will be transferred back to the location of the method call.

Page 28: 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

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:

Page 29: 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

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:

Page 30: 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

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

Page 31: 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

Example: how local variables are created and destroyed (cont.)

Example 1:

• The local variable m only exists within this green area:

Page 32: 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

Example: how local variables are created and destroyed (cont.)

Example 2:

• The local variable r only exists within this green area: