Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 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

2 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

3 Sample Method private static double fromDollar(double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount= dollar * fee; return amount; } Parameter Local Variables

4 Memory Allocation for Local Variables - 1 Code State of Memory A A A. A. Local variables do not exist before the method execution At before fromDollar A A finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

5 Memory Allocation for Local Variables - 2 Code B B State of Memory B. B. Memory space is allocated for the local variables and parameter. After is executed B B 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); }

6 Memory Allocation for Local Variables - 3 Code C C State of Memory C. C. Computed values are assigned to the local variables. After is executed C C 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); }

7 Memory Allocation for Local Variables - 4 Code D D State of Memory D. D. Memory space is deallocated upon exiting the fromDollar method. At after fromDollar D D finalAmount = fromDollar(200); private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); }

8 Pass-By-Value Scheme - 1 State of Memory private static void myMethod(int one, float two) { one = 25; two = 35.4f; } A A A. A. Local variables do not exist before the method execution At before myMethod A A Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20

9 Pass-By-Value Scheme - 2 State of Memory B. B. The values of arguments are copied to the parameters. Values are copied at B B Code x = 10; y = 20; myMethod( x, y ); B B xx 10 y 20 one 10 two 1020.0f private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

10 Pass-By-Value Scheme - 3 C C State of Memory C. C. The values of parameters are changed. After is executed C C Code x = 10; y = 20; myMethod( x, y ); xx 10 y 20 one 1025 two 1035.4f private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

11 Pass-By-Value Scheme - 4 Code D D State of Memory D. D. Parameters are erased. Arguments remain unchanged. At after myMethod D D x = 10; y = 20; myMethod( x, y ); xx 10 y 20 private static void myMethod(int one, float two) { one = 25; two = 35.4f; }

12 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 NonVariable Parameters.java

13 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.


Download ppt "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."

Similar presentations


Ads by Google