Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Object-Oriented Programming with Java--Wu

Similar presentations


Presentation on theme: "Introduction to Object-Oriented Programming with Java--Wu"— Presentation transcript:

1 Introduction to Object-Oriented Programming with Java--Wu
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 © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

2 Introduction to Object-Oriented Programming with Java--Wu
Parameters The formal parameters defined in the method header are 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 © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

3 Introduction to Object-Oriented Programming with Java--Wu
Chapter 4 Sample Method private static double fromDollar(double dollar) { double amount, fee; fee = exchangeRate - feeRate; amount = dollar * fee; return amount; } Parameter Local Variables © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

4 Memory Allocation for Local Variables - 1
Chapter 4 Memory Allocation for Local Variables - 1 Code A private static double fromDollar( double dollar) { double amount, rate; rate = EXCHANGE_RATE - FEE_RATE; amount = dollar * rate; return(amount); } finalAmount = fromDollar(200); At before fromDollar A A. Local variables do not exist before the method execution State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

5 Memory Allocation for Local Variables - 2
Chapter 4 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); After is executed B dollar amount 200.0 rate B. Memory space is allocated for the local variables and parameter. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

6 Memory Allocation for Local Variables - 3
Chapter 4 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 After is executed C dollar amount 200.0 rate C. Computed values are assigned to the local variables. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

7 Memory Allocation for Local Variables - 4
Chapter 4 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 At after fromDollar D D. Memory space is deallocated upon exiting the fromDollar method. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

8 Pass-By-Value Scheme - 1
Chapter 4 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; } At before myMethod A x 10 A. Local variables do not exist before the method execution y 10 20 State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

9 Pass-By-Value Scheme - 2
Chapter 4 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 Values are copied at B x 10 one 10 B. The values of arguments are copied to the parameters. y 10 20 two 10 20.0f State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

10 Pass-By-Value Scheme - 3
Chapter 4 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 After is executed C x 10 y 20 one 25 two 35.4f C. The values of parameters are changed. State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

11 Pass-By-Value Scheme - 4
Chapter 4 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 At after myMethod D x 10 D. Parameters are erased. Arguments remain unchanged. y 10 20 State of Memory © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu

12 Introduction to Object-Oriented Programming with Java--Wu
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 © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu

13 Arguments & Parameters: Points to Remember
Chapter 4 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. © 2000 McGraw-Hill Introduction to Object-Oriented Programming with Java--Wu Intro to OOP w/Java--Wu


Download ppt "Introduction to Object-Oriented Programming with Java--Wu"

Similar presentations


Ads by Google