Parameter variables definition how to recognize them life

Parameter variables: definition (how to recognize them), life time and scope.

Previously discusses: different kinds of variables • Java has 4 different kinds of variables (See: http: //download. oracle. com/javase/tutorial/java/nutsandbol ts/variables. html) • Class variables • Instance variables • Local variables • Parameter variables

Previously discusses: 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 My. Program { public static void main(String[] args) { // Body of method "main" double r; public class Tool. Box { 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 } r = My. Progran. min( 1. 0, 4. 0 ); System. out. println(r); r = My. Program. min( 3. 7, -2. 9 ); System. out. println(r); r = My. Program. 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 Tool. Box. min(1. 0, 4. 0):

Life time and scope of parameter variables (cont. ) It is as if the Tool. Box. min method adds the following definition of parameter variables:

Life time and scope of parameter variables (cont. ) • Now you can understand completely why the Tool. Box. 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:
- Slides: 19