Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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

2 Local variables Local variable: A local variable is used to store information that is relevant for the duration of the execution of one method

3 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)

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

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

6 Local variables (cont.) Next, we study the life time and the scoping rules for local variables

7 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

8 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)

9 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

10 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

11 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

12 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

13 Example: how local variables are created and destroyed Consider the following program:

14 Example: how local variables are created and destroyed (cont.) When the program starts to run, the RAM memory contains only the program instructions:

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

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

17 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

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

19 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)

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

21 When the computer executes the method call, the program execution is transferred, to the min method:

22 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)

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

24 Example: how local variables are created and destroyed (cont.) The return(m) statement will save the value of variable m in a return location:

25 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 )

26 Example: how local variables are created and destroyed (cont.) When program execution reaches the end of the function, the local variable m is destroyed:

27 Example: how local variables are created and destroyed (cont.) Furthermore, the program execution will be transferred back to the location of the method call.

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

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

30 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

31 Example: how local variables are created and destroyed (cont.) Example 1: The local variable m only exists within this green area:

32 Example: how local variables are created and destroyed (cont.) Example 2: The local variable r only exists within this green area:


Download ppt "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."

Similar presentations


Ads by Google