4.2 Local variables and how to recognize them Purpose of local variables • Local variable: • A local variable is used to store information that is relevant for the duration of the execution of one method • A local variable will exists as long as the method in which they have been been created is still running As soon as the method terminates (i.e., returns), all the local variables defined inside the method are destroyed. Defining (recognizing) local variables • 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) Defining (recognizing) 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); } } Defining (recognizing) 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); } } Defining (recognizing) local variables (cont.) • Next, we study the life time and the scoping rules for local variables Variables are of two type Variables declared inside a subroutine are local variables Variables declared outside subroutines are global or member variables they are members of the class Variables can be static (this chapter) or non-static (later…) Variables may be static or non-static Variables set in one subroutine can be used in another Member variables • Private: accessed inside or outside the class • Outside • <class-name>.<variable-name> • Out is a variable of system • System.out • Initialized with a default value • • • • Numeric types -> 0 Char -> Unicode Zero Strings -> null Boolean -> False Lets look at GuessingGame2 Parameters Parameter variables: definition (how to recognize them), life time and scope. Previously discussed: different kinds of variables • Java has 4 different kinds of variables (See: http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) • Class variables • Instance variables • Local variables • Parameter variables Previously discussed: different kinds of variables (cont.) • We have just learned about the local variables • In this webpage, we will discuss the parameter variables • Specifically: • How to identify (and define) parameter variables • The life time of parameter variables • The scope of parameter variables Purpose of parameter variables • Parameter variables: • A parameter variable is used to store information that is being passed from the location of the method call into the method that is called Purpose of parameter variables (cont.) • Illustration: Purpose of parameter variables (cont.) • Explanation: • Method 1 is about to call (invoke) method 2 • Method 1 has some information (stored in some variable inside method 1) that it want to pass (convey) to method 2 • The mechanism used to accomplish this passing of information are parameter variables Purpose of parameter variables (cont.) • Example: Purpose of parameter variables (cont.) • Explanation: • Method main is calling (invoke) the method min • Method main has some information (the values 1.0 and 4.0) that it want to pass (convey) to method min • The mechanism used to accomplish this passing of information uses the parameter variables a and b Defining (recognizing) parameter variables • How to the define (and recognize) a parameter variable: • A parameter variable is defined inside the brackets ( ... ) of the header of a method Defining (recognizing) parameter variables (cont.) • Example: public class MyProgram { public static void main(String[] args) { // Body of method "main" public class ToolBox { public static double min ( double a, double b ) { // Body of method "min" double m = 0; // *** Local variable double r; // *** Local variable if ( a < b ) { m = a; // a is the smaller value } else { m = b; // b is the smaller value } 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); return(m); } } } } Defining (recognizing) parameter variables (cont.) • Comments: • The variable args in method main is a parameter variable Its type is String[] (will be explained much later...) • The variables a and b in method min are 2 parameter variable Their type is double (you should know what that means...) Life time and scope of parameter variables • Simple rule to go by: • A parameter variable behaves like a local variable that is defined at the start of the method • In addition: • The value of the parameter variable is always initialized by the method invocation mechanism using the values inside the call Life time and scope of parameter variables (cont.) • Example: • The method call ToolBox.min(1.0, 4.0): Life time and scope of parameter variables (cont.) It is as if the ToolBox.min method adds the following definition of parameter variables: Life time and scope of parameter variables (cont.) • Now you can understand completely why the ToolBox.min method will return the value 1.0 !!! (Because it used parameter variable with values 1.0 and 4.0 - so it will return the smaller value which is 1.0) Summary: life time and scope of parameter variables • Life time of a parameter variable: • is the entire body of the method Summary: life time and scope of parameter variables (cont.) • It is as if as follows: Summary: life time and scope of parameter variables (cont.) • Scope of a parameter variable: • is also the entire body of the method Summary: life time and scope of parameter variables (cont.) • It is as if as follows: Parameters II Parameter passing mechanism: passby-value Introduction • In the last webpage, we discussed how to pass information to a method • I have kept it (deliberately) simple by using constant values as parameters: r = ToolBox.min( 1.0, 4.0 ); • In this webpage, we will discuss how to pass information stored in variables to a method. Specifically, we will study the pass-by-value mechanism Example: passing information stored in variables • Consider the following program: Example: passing information stored in variables (cont.) • Question to ponder: • How can we pass (give) the information stored inside the variables x and y to the method ToolBox.min There are quite a few ways to allow (enable) you to accomplish this "passing" The possible answers ranges from simple to pretty weird Parameter passing mechanisms • Definition: • Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them Parameter passing mechanisms (cont.) • Important note: • Both the calling method and the called method must agree to use the same passing mechanism (or else, the information will be passed incorrectly) Parameter passing mechanisms (cont.) • Most commonly used parameter passing mechanisms: Pass-by-value The calling method passes the information stored inside a variable by passing (= copying) the value contained inside a variable into the parameter variable. Parameter passing mechanisms (cont.) This is the most obvious way to pass information... Example: • if you want to give you phone number of your home to someone, you make a copy of the information (in the parameter variable) Parameter passing mechanisms (cont.) Pass-by-reference • The calling method passes the information stored inside a variable by passing (= copying) the address (location) of a variable into the parameter variable. Parameter passing mechanisms (cont.) This is a less obvious but more powerful way to pass information... Example: • if you want to give you phone number of your home to someone, you make a copy of the address of your home (in the parameter variable) He/she can find the phone number by visiting that address !!! Parameter passing mechanisms (cont.) • Terminology: • A reference in Computer Science is the location (or address) (of a variable or a method) Terminology: formal parameters and actual parameters • Definitions: • Formal parameter = a parameter variable • Actual parameter = a variable whose value is to be passed to some formal parameter Terminology: formal parameters and actual parameters (cont.) • Illustrated example: Terminology: formal parameters and actual parameters (cont.) • Explanation: • The parameter variables a and b in the definition of the ToolBox.min method are formal parameters • The variables x and y used in the method invocation ToolBox.min(x, y) are actual parameters The Pass-by-value mechanism - the agreement • Recall: • Parameter passing mechanism = agreement between the calling method and the called method on how a parameter is passed between them The Pass-by-value mechanism - the agreement (cont.) • The agreement used in the Pass-by-value mechanism: • For the calling method: • The calling method creates the parameter variables for the called method, .... and • The calling method copies the value of the actual parameter into the formal parameter The Pass-by-value mechanism - the agreement (cont.) • For the called method: • The called method obtains the information directly from the parameter variables The Pass-by-value mechanism - an example • Example program: The Pass-by-value mechanism - an example (cont.) • When main starts running, it will first create its local variables: The Pass-by-value mechanism - an example (cont.) • When execution reaches the method call ToolBox.min(x,y), the Pass-by-value mechanism first creates the parameter variables: The Pass-by-value mechanism - an example (cont.) • Then the Pass-by-value mechanism copies the value of the actual parameter to the corresponding formal parameter: The Pass-by-value mechanism - an example (cont.) • The method invocation mechanism is completed as usually with the following steps: • Save the return address on the stack: The Pass-by-value mechanism - an example (cont.) • Jump to the called method: The Pass-by-value mechanism - an example (cont.) • When the min method executes, it will create its local variable m: The Pass-by-value mechanism - an example (cont.) • Notice how the called method uses the parameter variables: • When the called method uses a parameter variable, the information is obtained directly from the parameter variable: A quiz on the Pass-by-value mechanism • Consider the following program: A quiz on the Pass-by-value mechanism (cont.) • Questions: • What value is printed by the statement System.out.println(x); ? • What value is printed by the statement System.out.println(y); ? • What value is printed by the statement System.out.println(r); ? A quiz on the Pass-by-value mechanism (cont.) • Example Program: (Demo above code) • Prog file 1: http://www.tacomacc.edu/home/ebasham/CS142/toolbox.java.txt - Prog file 2: http://www.tacomacc.edu/home/ebasham/CS142/toolbox.java.txt ToolBox.java.txt What is the output? public class MyProgram class ToolBox { { public static void main(String[] args) public static double fun ( double a, double b ) { { double x = 1.0, y = 4.0;; double m = 0; double r; a = a + 1; r = ToolBox.fun( x, y ); b = b + 2; m = a + b; System.out.println(x); System.out.println(y); return(m); // Output of the method System.out.println(r); } } } } A quiz on the Pass-by-value mechanism (cont.) • Output of the program: 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= 2.0 + 6.0) Did you understand why the update statements "a = a + 1" and "b = b + 2" did not update the actual parameters x and y ??? The quiz explained • Notice the similarities between the ToolBox.min and the ToolBox.fun methods: public static double min ( double a, double b ) { double m = 0; if ( a < b ) { m = a; } else { m = b; } return(m); } public static double fun ( double a, double b ) { double m = 0; a = a + 1; b = b + 2; m = a + b; return(m); } The quiz explained (cont.) • Both methods have 2 parameter variables and 1 local variable • I have constructed the quiz in such a way that I can re-use the diagrams from the Pass-by-value example above. The quiz explained (cont.) • So according to the Pass-by-value example above, when the ToolBox.min method starts running, the following variables have been created on the System Stack: The quiz explained (cont.) • Notice that: • The local variables x and y in the main method and • The parameter variables a and b in the fun method are different variables (they occupy different memory cells !) The quiz explained (cont.) • The assignment statements: a = a + 1; b = b + 2; will change the values of the parameter variables: The quiz explained (cont.) The quiz explained (cont.) • Notice that: • The values in the actual parameters (x and y) are unchanged !!! The quiz explained (cont.) • That's why the statements System.out.println(x); ---> prints 1.0 System.out.println(y); ---> prints 4.0 The quiz with an additional twist... • Now, consider the following program: The quiz with an additional twist... (cont.) • We use the same names for actual and formal parameters !!! • Questions: • What value is printed by the statement System.out.println(a); ? • What value is printed by the statement System.out.println(b); ? • What value is printed by the statement System.out.println(r); ? The quiz with an additional twist... (cont.) • Example Program: (Demo above code) The quiz with an additional twist... (cont.) • Output of the program: 1.0 (the value in x is UNCHANGEDD !) 4.0 (the value in y is UNCHANGEDD !) 8.0 (= 2.0 + 6.0) Did you understand why the update statements "a = a + 1" and "b = b + 2" (that updates the formal parameters) did not update the actual parameters a and b ??? The quiz explained (cont.) • In other words: • The local variables named a and b defined inside the main method and • The parameter variables named a and b defined inside the fun method are different variables The quiz explained (cont.) • The following diagram shows the fact that there are 2 different variables with the same name created created on the System Stack: The quiz explained (cont.) • Notice that: • The blue colored variables named a and b are inside the scope of the main method and • The magenta colored variables named a and b are inside the scope of the fun method are different variables --- it's possible because of the scopes are non-overlapping (furthermore, they use different memory cells !) The quiz explained (cont.) • The assignment statements: a = a + 1; b = b + 2; will change the values of the parameter variables: The quiz explained (cont.) The quiz explained (cont.) • Notice that: • The values in the actual parameters (a and b) are unchanged !!! The quiz explained (cont.) • That's why the statements System.out.println(a); ---> prints 1.0 System.out.println(b); ---> prints 4.0