1 / 13

Local Variables

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.

smickey
Télécharger la présentation

Local Variables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

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

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

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

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

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

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

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

  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 NonVariableParameters.java

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

More Related