Announcements - PowerPoint PPT Presentation

1 / 26
About This Presentation
Title:

Announcements

Description:

b. 7. d. CS100. Lecture 7. 6. Method Invocation ... b= max (3*20, b 5) 2; The state before execution, with b = 7: ... b= max (3*20, b 5) 2; ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 27
Provided by: Mill1
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Homework P1 due tomorrow (Thursday)
  • Homework P2 has been handed out and is on the web

2
Todays Topics
  • Review
  • Method calls in (excruciating -) ) detail.

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

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.

5
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

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

7
Example
  • // Yield the maximum of x and y
  • public int max(int x, int y)
  • int z
  • z x
  • if (y gt x)
  • z y
  • return z
  • The call is in the assignment given below.
  • int b
  • b max (320, 65) 2

8
  • b max (320, b5) 2
  • The state before execution, with b 7
  • The state after execution of step 0.
  • The state after execution of step 1.

9
  • Performing step 2, initial state
  • After execution of z x
  • After execution of the conditional statement

10
  • Executing the return, which terminates execution
    of the call (perform step 3).
  • 60
  • b max (320, b5) 2
  • Memorize the steps in executing a procedure
    call, given on slide 3. Practice executing simple
    procedure calls yourself.

11
  • // Add b to c
  • public void addC(Coordinate b, Coordinate c)
  • c.x c.xb.x
  • c.y c.yb.y
  • Execute the call
  • addC(d,b)
  • assuming the initial state is

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

13
  • Execution of step 1 yields

14
  • Execution of step 2 yields

15
  • Execution of step 3 yields

16
Execution of a new Coordinate constructor
  • public class Coordinate
  • public int x
  • public int y
  • // Constructor an instance with x b and y0
  • 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)

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

18
  • 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!

19
  • 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)
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, its
    parameter is a copy of whatever value was being
    passed
  • The method does not change the original
    variables value

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)

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

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

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)

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 16System.out.println(a
    a a)

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))
Write a Comment
User Comments (0)
About PowerShow.com