Download presentation
Presentation is loading. Please wait.
Published byMerryl Small Modified over 9 years ago
1
Methods & Activation Record
2
Recap: what’s a method? http://codingbat.com/java
3
Method Headers public static void main(String[] args) {}
4
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);
5
Scope What happens in the curly braces, Stays in the curly braces. How does this work with methods?
6
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; }
7
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
8
addFive() main() Scanner sc double a double sum = ? public static void addFive() { double sum = a + 5; } String[] args ?!?!?!?!?!?!?!?!
9
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; }
10
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
11
addFive() main() Scanner sc double a double sum = ? public static void addFive(double a) { double sum = a + 5; } String[] args double 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
12
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
13
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; }
14
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
15
addFive() main() Scanner sc double a double sum = public static int addFive(double a) { return a + 5; } String[] args double a a + 5
16
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 a + 5
17
Let’s draw the stack frame a bit cleaner. Local Variables Return address Parameters
18
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; }
19
int i = 0 int product = 0 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY
20
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; }
21
int product = 50 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY
22
Another example public 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; }
23
int ret; double dec = 0.56; [address of main()] double d = 54.56 double r = 54.56 [address in system] String[] args MAIN() ROUND()
24
Another example public 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; }
25
int p = 24 int r = 12 [address of A()] int i = 12 [address of main()] int i = 10 [address in system] String[] args MAIN() A() B()
26
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.