Methods & Activation Record
Recap: what’s a method?
Method Headers public static void main(String[] args) {}
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);
Scope What happens in the curly braces, Stays in the curly braces. How does this work with methods?
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; }
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
addFive() main() Scanner sc double a double sum = ? public static void addFive() { double sum = a + 5; } String[] args ?!?!?!?!?!?!?!?!
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; }
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
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
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
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; }
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
addFive() main() Scanner sc double a double sum = public static int addFive(double a) { return a + 5; } String[] args double a a + 5
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
Let’s draw the stack frame a bit cleaner. Local Variables Return address Parameters
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; }
int i = 0 int product = 0 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY
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; }
int product = 50 [address of main()] int b = 10 int a = 5 [address in system] String[] args MAIN MULTIPLY
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; }
int ret; double dec = 0.56; [address of main()] double d = double r = [address in system] String[] args MAIN() ROUND()
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; }
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()
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