26
Methods & Activation Record

Methods & Activation Record. Recap: what’s a method?

Embed Size (px)

Citation preview

Page 1: Methods & Activation Record. Recap: what’s a method?

Methods & Activation Record

Page 2: Methods & Activation Record. Recap: what’s a method?

Recap: what’s a method?

http://codingbat.com/java

Page 3: Methods & Activation Record. Recap: what’s a method?

Method Headers

public static void main(String[] args) {}

Page 4: Methods & Activation Record. Recap: what’s a method?

Method Headers

public class Car {

public double getSpeed();

}

public class Math {

public static double abs(double a);

}

Car c = new Car();

double s = c.getSpeed();

double a = Math.abs(-4);

Page 5: Methods & Activation Record. Recap: what’s a method?

Scope

What happens in the curly braces,

Stays in the curly braces.

How does this work with methods?

Page 6: Methods & Activation Record. Recap: what’s a method?

Example Code: Does this work?

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive();

}

public static void addFive() {

double sum = a + 5;

}

Page 7: Methods & Activation Record. Recap: what’s a method?

main()

Scanner sc

double a

double sum = ?

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(); // executing this line

}

String[] args

Page 8: Methods & Activation Record. Recap: what’s a method?

addFive()

main()

Scanner sc

double a

double sum = ?

public static void addFive() {

double sum = a + 5;

}

String[] args

?!?!?!?!?!?!?!?!

Page 9: Methods & Activation Record. Recap: what’s a method?

Let’s fix that…

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a);

}

public static void addFive(double a) {

double sum = a + 5;

}

Page 10: Methods & Activation Record. Recap: what’s a method?

main()

Scanner sc

double a

double sum = ?

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a); // executing this line

}

String[] args

Page 11: Methods & Activation Record. Recap: what’s a method?

addFive()

main()

Scanner sc

double a

double sum = ?

public static void addFive(double a) {

double sum = a + 5;

}

String[] argsdouble a

Note that main() still has the original variable a – addFive() just has a COPY. We call this “pass by value.”

double sum = a + 5

Page 12: Methods & Activation Record. Recap: what’s a method?

main()

Scanner sc

double a

double sum = ?

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a); // we’re back to this line

}

String[] args

Page 13: Methods & Activation Record. Recap: what’s a method?

Example Code

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a);

}

public static double addFive(double a) {

return a + 5;

}

Page 14: Methods & Activation Record. Recap: what’s a method?

main()

Scanner sc

double a

double sum =

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a); // executing this line

}

String[] args

Page 15: Methods & Activation Record. Recap: what’s a method?

addFive()

main()

Scanner sc

double a

double sum =

public static int addFive(double a) {

return a + 5;

}

String[] argsdouble a

a + 5

Page 16: Methods & Activation Record. Recap: what’s a method?

main()

Scanner sc

double a

double sum =

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

double a = sc.nextDouble();

double sum = addFive(a); // we’re back to this line

}

String[] argsa + 5

Page 17: Methods & Activation Record. Recap: what’s a method?

Let’s draw the stack frame a bit cleaner.

Local Variables

Return addressParameters

Page 18: Methods & Activation Record. Recap: what’s a method?

Now get out a sheet of paper…

public static void main(String[] args) {

int product = multiply(5, 10);

}

public static int multiply(int a, int b) {

int product = 0;

for (int i = 0; i < b; ++i) // at i = 0

product += a;

return product;

}

Page 19: Methods & Activation Record. Recap: what’s a method?

int i = 0int product = 0

[address of main()]int b = 10int a = 5

[address in system]String[] argsM

AIN

MU

LTIP

LY

Page 20: Methods & Activation Record. Recap: what’s a method?

What about now?

public static void main(String[] args) {

int product = multiply(5, 10);

}

public static int multiply(int a, int b) {

int product = 0;

for (int i = 0; i < b; ++i)

product += a;

return product;

}

Page 21: Methods & Activation Record. Recap: what’s a method?

int product = 50[address of main()]

int b = 10int a = 5

[address in system]String[] argsM

AIN

MU

LTIP

LY

Page 22: Methods & Activation Record. Recap: what’s a method?

Another examplepublic static void main(String[] args) {

double r = 54.56;

int num = round(r);

}

public static int round(double d) {

double dec = d – (int) d;

int ret;

if (dec < 0.5)

ret = Math.floor(d);

else

ret = Math.ceil(d);

return ret;

}

Page 23: Methods & Activation Record. Recap: what’s a method?

int ret;double dec = 0.56;[address of main()]double d = 54.56double r = 54.56

[address in system]String[] argsM

AIN

()R

OU

ND

()

Page 24: Methods & Activation Record. Recap: what’s a method?

Another examplepublic static void main(String[] args) {

int num = A(10);

}

public static int A(int i) {

return B(i + 2);

}

public static int B(int i) {

int r = i;

int p = r + i;

return i + p + r;

}

Page 25: Methods & Activation Record. Recap: what’s a method?

int p = 24

int r = 12[address of A()]

int i = 12[address of main()]

int i = 10[address in system]

String[] argsMA

IN()

A()

B()

Page 26: Methods & Activation Record. Recap: what’s a method?

To Sum Up

• Scope – What happens in the curly braces, stays in the curly braces

• This concept translates into chunks of memory

• These chunks get pushed onto a stack and get popped off when there’s a “return” or the method ends

• Note that we’ve only been passing primitive types

• Something different happens with complex types called pass by reference