1 / 26

Announcements

This lecture covers method calls and execution, including the scope of variables, fields in classes, and parameters of methods. Learn how to execute method calls and understand the terminology of parameters and arguments. Includes examples and practice exercises.

Download Presentation

Announcements

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. Announcements • Homework P1 due tomorrow (Thursday) • Homework P2 has been handed out and is on the web Lecture 7

  2. Today’s Topics • Review • Method calls in (excruciating :-) ) detail. Lecture 7

  3. Review • Overview of material so far • Scope of variables • fields in classes • parameters of methods • local variables, variables in blocks • The while loop Lecture 7

  4. Method Calls • Today, we will describe how a method call is executed • You should know this material very well • Many difficulties in programming come from not understanding exactly how statements, method calls, etc. are being executed. Lecture 7

  5. instance of class C c __4__ d __3__ b Executing an Assignment • To execute an assignment x= e;(1) Evaluate e (yielding a value v, say)(2) Store v in the variable described by x. • b.c = 2 * b.c value is 8, so store 8 in field c of b • b = new C(8, 7) value is name of new instance of class C, so store that in b d 7 Lecture 7

  6. Method Invocation • 0. Allocate a new set of locations to contain the parameters and local variables of the method being called, in what we call a frame. • 1. Assign the arguments of the call to the parameters of the method. • 2. Execute the procedure body. • 3. Delete the frame produced in step 0; if the method is a function, return the value of the function to the place of call. • Terminology: parameter: formal parameter argument: actual parameter Lecture 7

  7. Example // Yield the maximum of x and y publicint max(int x, int y) {int z; z= x; if (y > x) z= y; return z; } The call is in the assignment given below. int b; b= max (3*20, 6+5) + 2; Lecture 7

  8. y x b z z x y b b ? ? ? 60 12 7 7 ? 7 frame for the call frame for the call b= max (3*20, b+5) + 2; The state before execution, with b = 7: The state after execution of step 0. The state after execution of step 1. Lecture 7

  9. x b x y z y x z y z b b 12 7 ? 60 7 7 60 12 60 60 12 60 frame for the call frame for the call frame for the call Performing step 2, initial state After execution of z= x; After execution of the conditional statement: Lecture 7

  10. b b 7 62 Executing the return, which terminates execution of the call (perform step 3). 60 b= max (3*20, b+5) + 2; Memorize the steps in executing a procedure call, given on slide 3. Practice executing simple procedure calls yourself. Lecture 7

  11. Coordinate Coordinate x b x d y y 5 1 3 4 // Add b to c publicvoid addC(Coordinate b, Coordinate c) {c.x= c.x+b.x; c.y= c.y+b.y; } Execute the call addC(d,b); assuming the initial state is: Lecture 7

  12. frame for call c b ? ? Coordinate Coordinate x b d x y y 5 1 3 4 Step 0: allocate a frame for the method Note that, below, there are two variables named b. There is no ambiguity. Later, when executing the method body, always use the variables in the frame for the call. Lecture 7

  13. frame for call b c Coordinate Coordinate b x d x y y 1 5 4 3 • Execution of step 1 yields: Lecture 7

  14. frame for call b c Coordinate Coordinate b x d x y y 1 6 4 7 • Execution of step 2 yields: Lecture 7

  15. Coordinate Coordinate x b x d y y 6 1 7 4 • Execution of step 3 yields: Lecture 7

  16. Execution of a new Coordinate & constructor public class Coordinate {public int x; public int y; // Constructor: an instance with x= b and y=0 public Coordinate(int b) {x= b; y= 0;}} Coordinate d; d= new Coordinate(9); To evaluate new Coordinate(9): • 0. Create a new instance of Coordinate • 1. Execute the call Coordinate(9) Lecture 7

  17. d ? Coordinate ? ? y x Coordinate (constructor) Initial State and Step 0 Initial state: Execution of step 0 yields the following. Note that we have placed method Coordinate within the instance. The instance contains all the fields and methods defined within the class. This will be important when executing the method body, as shown below, to follow the scope rules. Lecture 7

  18. Coordinate Coordinate ? ? ? ? y y x x Coordinate (constructor) Coordinate (constructor) frame for call frame for call b b ? 9 Step 1: Execute the call on the constructor. Rule: when drawing the frame, place the frame within the class instance that contains the called method! Lecture 7

  19. Coordinate 9 9 0 y x x Coordinate (constructor) frame for call b 9 After execution of constructor body After call is completed: d= new Coordinate(9); So the name of this instance is stored in d. Coordinate 0 y Coordinate (constructor) Lecture 7

  20. Call by Value • All of this is to say that all parameters to Java methods are “call by value” • If you pass a boolean, e.g. to a method, it’s parameter is a copy of whatever value was being passed • The method does not change the original variable’s value Lecture 7

  21. Example -- call by value Class passByValue { public static void main(String[] args) { double one = 1.0 System.out.println(“before: one = ” + one); halveIt(one); System.out.println(“after: one = ” + one); } public static void halveIt(double arg) { arg = arg / 2.0; // divide arg by two System.out.println(“halved: arg = ” + arg); } } Lecture 7

  22. Slightly different for objects class passRefByValue { public static void main(String[] args) { Body sirius = new Body(“Sirius, null); System.out.println(“before: ” + sirius); commonName(sirius): System.out.println(“after: ” + sirius); } public static void commonName(Body bodyRef) { bodyRef.name = “Dog Star”; bodyRef = null; } } Lecture 7

  23. Output of previous before: 0 (Sirius) after: 0 (Dog Star) • The contents of the object have been modified, but. . . • . . .the reference bodyRef still refers to the Body object, even though commonName changed its value to null • Remember: aliases of objects Lecture 7

  24. this -- briefly • Commonly used to pass a reference to the current object to other methods • The this reference always refers the current object that is executing the code • Suppose you want to add the current object to a list of objects w/in a method called on that object: Service.add(this); Lecture 7

  25. Increment and Decrement • ++ and -- • a++ is equivalent to a = a + 1 • ++a is equivalent to a = a + 1 • Difference is when the value is returned: if prefix, the operation is applied before the value is return, if postfix, after • Examplea = 16;System.out.println(++a + “ ” + a++ + “ ” + a); Lecture 7

  26. Assignment Operators • Any arithmetic or binary operator can be concatenated with = to form another assignment operator • a *= b + 1; // same as a = a * (b + 1) • a += 5; // same as a = a + 5; • var op= expr; is equivalent tovar = ((var) op (expr)); Lecture 7

More Related