130 likes | 257 Views
Learn about local variables and parameters, memory allocation, passing by value, and the difference between formal and actual parameters in Java methods.
E N D
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; } Parameter Local Variables
A At before fromDollar A Memory Allocation for Local Variables - 1 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); A. Local variables do not exist before the method execution State of Memory
After is executed B dollar 200.0 amount rate Memory Allocation for Local Variables - 2 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } B finalAmount = fromDollar(200); B. Memory space is allocated for the local variables and parameter. State of Memory
After is executed C dollar 200.0 amount 24846.3 rate 124.2315 Memory Allocation for Local Variables - 3 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); C C. Computed values are assigned to the local variables. State of Memory
At after fromDollar D Memory Allocation for Local Variables - 4 Code private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); D D. Memory space is deallocated upon exiting the fromDollar method. State of Memory
At before myMethod A x x 10 10 10 20 y Pass-By-Value Scheme - 1 Code A x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } A. Local variables do not exist before the method execution State of Memory
Values are copied at B x x one 10 10 20 10 10 10 two y 20.0f 10 Pass-By-Value Scheme - 2 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } B B. The values of arguments are copied to the parameters. State of Memory
C After is executed C x x one 20 10 10 25 10 10 two y 35.4f 10 Pass-By-Value Scheme - 3 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } C. The values of parameters are changed. State of Memory
At after myMethod D x x 10 10 10 20 y Pass-By-Value Scheme - 4 Code x = 10; y = 20; myMethod( x, y ); private static void myMethod(int one, float two) { one = 25; two = 35.4f; } D D. Parameters are erased. Arguments remain unchanged. State of Memory
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 • Arguments are passed to a method using the pass-by-value scheme. • 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. • The number of arguments in the method call must match the number of parameters in the method definition. • Parameters and arguments do not have to have the same name. • Local copies, which are distinct from arguments, are created even if the parameters and arguments share the same name. • 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.