Upload
brook-osborne
View
225
Download
0
Tags:
Embed Size (px)
Citation preview
Local Variables
A local variable is a variable that is declared within a method declaration.
Local variables are accessible only from the method in which they are declared.
Memory space for local variables are allocated only during the execution of the method. When the method execution completes, memory space will be deallocated.
Grukkee … out to reality LocalVariables.java
Parameters
The formal parameters defined in the method header are also local variables
Formal parameters receive copies of the actual parameters passed in the call
The values are copied in order, 1st to 1st, 2nd to 2nd, etc
Ooodles … out to reality Parameters.java
Sample Method
private static double fromDollar(double dollar)
{
double amount, fee;
fee = exchangeRate - feeRate;
amount = dollar * fee;
return amount;
}
ParameterParameter
Local Variables
Local Variables
Memory Allocation for Local Variables - 1
Code
State of Memory
AA
A. A. Local variables do not exist before the method execution
A. A. Local variables do not exist before the method execution
At before fromDollarAA
finalAmount = fromDollar(200);
private static double fromDollar( double dollar) { double amount, rate;
rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate;
return(amount);}
Memory Allocation for Local Variables - 2
CodeBB
State of Memory
B. B. Memory space is allocated for the local variables and parameter.
B. B. Memory space is allocated for the local variables and parameter.
After is executedBB
dollar
amount
200.0
rate
finalAmount = fromDollar(200);
private static double fromDollar( double dollar) { double amount, rate;
rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate;
return(amount);}
Memory Allocation for Local Variables - 3
Code
CC
State of Memory
C. C. Computed values are assigned to the local variables.
C. C. Computed values are assigned to the local variables.
After is executedCC
dollar
amount
200.0
rate
24846.3
124.2315
finalAmount = fromDollar(200);
private static double fromDollar( double dollar) { double amount, rate;
rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate;
return(amount);}
Memory Allocation for Local Variables - 4
Code
DD
State of Memory
D. D. Memory space is deallocated upon exiting the fromDollar method.
D. D. Memory space is deallocated upon exiting the fromDollar method.
At after fromDollarDD
finalAmount = fromDollar(200);
private static double fromDollar( double dollar) { double amount, rate;
rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate;
return(amount);}
Pass-By-Value Scheme - 1
State of Memory
private static void myMethod(int one, float two) { one = 25; two = 35.4f;}
AA
A. A. Local variables do not exist before the method execution
A. A. Local variables do not exist before the method execution
At before myMethodAA
Codex = 10;y = 20;myMethod( x, y );
xx 1010
y 1020
Pass-By-Value Scheme - 2
State of Memory
B. B. The values of arguments are copied to the parameters.
B. B. The values of arguments are copied to the parameters.
Values are copied at BB
Codex = 10;y = 20;myMethod( x, y ); BB
xx 1010
y 1020
one 1010
two 1020.0f
private static void myMethod(int one, float two) { one = 25; two = 35.4f;}
Pass-By-Value Scheme - 3
CC
State of Memory
C. C. The values of parameters are
changed.
C. C. The values of parameters are
changed.
After is executedCC
Codex = 10;y = 20;myMethod( x, y );
xx 1010
y 1020
one 1025
two 1035.4f
private static void myMethod(int one, float two) { one = 25; two = 35.4f;}
Pass-By-Value Scheme - 4
Code
DD
State of Memory
D. D. Parameters are erased. Arguments remain unchanged.
D. D. Parameters are erased. Arguments remain unchanged.
At after myMethodDD
x = 10;y = 20;myMethod( x, y );
xx 1010
y 1020
private static void myMethod(int one, float two) { one = 25; two = 35.4f;}
Actual Parameters
Formal and actual parameters need not (but can) have the same name.
Changes to the formal parameters have no effect in the calling code because they are local!
Actual parameters need not be variables.
Ack … out to reality NonVariableParameters.java
Arguments & Parameters: Points to Remember
1. Arguments are passed to a method using the pass-by-value scheme.
2. Arguments are matched to the parameters from left to right. The data type of an argument must be assignment compatible to the data type of the matching parameter.
3. The number of arguments in the method call must match the number of parameters in the method definition.
4. Parameters and arguments do not have to have the same name.
5. Local copies, which are distinct from arguments, are created even if the parameters and arguments share the same name.
6. Parameters are input to a method, and they are local to the method. Changes made to the parameters will not affect the value of corresponding arguments.