|
We have just learned about the local variables
In this webpage, we will discuss the parameter variables
Specifically:
|
|
Example:
public class Class1 { public static void method1() { double x; // x contains // some information .... Class2.method2( x ); // Pass (give) information // stored in x to method2 !!! .... } .... } |
public class Class2 { public static void method2( double paramVar ) { statements that operate on paramVar .... } .... } |
Effect: the statement Class2.method2( x ) pass (= copy) the information stored in x into the parameter variable paramVar
Explanation:
|
|
Explanation:
|
|
Example:
public class MyProgram { public static void main(String[] args) { // Body of method "main" double r; // *** Local variable r = MyProgram.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); } } |
Comments:
|
|
Example:
Therefore, parameter variables has the same life time as local variables that are defined at the start of the method.
In other words, you can look at parameter variable like this:
|
Example:
Just like the life time, parameter variables has the same scope as local variables that are defined at the start of the method.
In other words, you can look at parameter variable like this:
|
|
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)
|
In other words:
|
|
public class MyProgram { public static double min ( double s, double r ) { double m = 0; if ( s < r ) { m = s; // s is the smaller value } else { m = r; // r is the smaller value } return(m); // Output of the method } public static void main(String[] args) { double r; r = min( 1.0, 4.0 ); System.out.println(r); r = min( 3.7, -2.9 ); System.out.println(r); r = min( -9.9, 3.8 ); System.out.println(r); } } |
Note:
|
How to run the program:
|