Transcript .ppt

Announcements
• Homework P1 due tomorrow (Thursday)
• Homework P2 has been handed out and is
on the web
CS100
Lecture 7
1
Today’s Topics
• Review
• Method calls in (excruciating :-) ) detail.
CS100
Lecture 7
2
Review
• Overview of material so far
• Scope of variables
– fields in classes
– parameters of methods
– local variables, variables in blocks
• The while loop
CS100
Lecture 7
3
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.
CS100
Lecture 7
4
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.
instance of class C
b
d
7
c __4__
d __3__
• 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
CS100
Lecture 7
5
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
CS100
Lecture 7
6
Example
// Yield the maximum of x and y
public int 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;
CS100
Lecture 7
7
b= max (3*20, b+5) + 2;
The state before execution, with b = 7:
b
7
The state after execution of step 0.
b
7
frame for the call
x
?
y
?
The state after execution of step 1.
b
7
x
CS100
60
y
12
Lecture 7
z
?
frame for the call
z
?
8
Performing step 2, initial state b
x
60
After execution of z= x;
x
7
60
y
b
12
7
y
frame for the call
z
?
frame for the call
12
z
60
After execution of the conditional statement:
b
7
frame for the call
x
CS100
60
y
Lecture 7
12
z
60
9
Executing the return, which terminates execution of the call (perform
step 3).
b
7
60
b= max (3*20, b+5) + 2;
b
62
Memorize the steps in executing a procedure call, given on
slide 3. Practice executing simple procedure calls yourself.
CS100
Lecture 7
10
// Add b to c
public void 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:
Coordinate
b
x
3
Coordinate
d
x
CS100
y
5
1
Lecture 7
y
4
11
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.
Coordinate
b
x
5
y
3
Coordinate
d
x
1
y
4
frame for call
b
CS100
?
Lecture 7
c
?
12
• Execution of step 1 yields:
Coordinate
b
x
y
5
3
Coordinate
d
x
1
y
4
frame for call
b
CS100
c
Lecture 7
13
• Execution of step 2 yields:
Coordinate
b
x
y
6
7
Coordinate
d
x
1
y
4
frame for call
b
CS100
c
Lecture 7
14
• Execution of step 3 yields:
Coordinate
b
x
6
7
Coordinate
d
x
CS100
y
1
Lecture 7
y
4
15
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)
CS100
Lecture 7
16
Initial State and Step 0
Initial state:
d
?
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.
Coordinate
x
?
y
?
Coordinate (constructor)
CS100
Lecture 7
17
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!
Coordinate
x
y
?
?
Coordinate (constructor)
frame for call
b
Coordinate
?
x
y
?
?
Coordinate (constructor)
frame for call
b
CS100
Lecture 7
9
18
After execution of constructor body
Coordinate
y
9
x
0
Coordinate (constructor)
frame for call
b
9
After call is completed: d= new Coordinate(9);
Coordinate
x
9
y
0
Coordinate (constructor)
So the name of this instance is stored in d.
CS100
Lecture 7
19
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
CS100
Lecture 7
20
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);
}
CS100
Lecture 7
}
21
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;
}
}
CS100
Lecture 7
22
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
CS100
Lecture 7
23
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);
CS100
Lecture 7
24
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
• Example
a = 16;
System.out.println(++a + “ ” + a++ + “ ” + a);
CS100
Lecture 7
25
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 to
var = ((var) op (expr));
CS100
Lecture 7
26