Benefits of Methods Chapter 5 Methods • Write a method once and reuse it anywhere. • Information hiding: hide the implementation from the user • Reduce complexity 1 4 Motivating Example Defining and Using Methods Often we need to find the maximum between two Define a method – give a definition of what the method is to do numbers modifier returnType methodName(list of parameters) { collection of statements; int result; } if (num1 > num2) Call or invoke a method – use a method result = num1; methodName(list of parameters) else result = num2; A method is a construct for grouping statements together to perform a function. A method is defined and implemented once but can be used repeatedly A method can be used as a blackbox 5 Method Abstraction Return Value Type Method abstraction a black box that contains the detailed A method may return a value (int, double, char, String, implementation for the method. …) – value-returning method A method may perform desired operations without returning a value (void) – void method 3 6
Method Signature main method Method signature The main method is a method that’s invoked by JVM The combination of the method name and the parameter list The main method’s header is always the same public static void main(String[] args) { //method body } The statements in main method may invoke other methods that are defined in the class or in other classes System.out.println(“Hello World!”); int number = (int) (Math.random()*100); 7 Parameters Example The variables defined in the method header are known as formal parameters A program that defines and uses a method max to When invoking a method, a value is passed to the parameter and this value is return the larger of the two int values referred to as actual parameter or argument . The arguments must match the parameters in order, number, and compatible type Parameters are optional TestMax.java 8 11 Method Body Calling Methods The method body contains a collection of statements A return statement is required for a value-returning pass the value of i method pass the value of j public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 9 12
Trace Method Invocation Trace Method Invocation i is now 5 invoke max(i, j) Pass the value of i to num1 Pass the value of j to num2 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; public static void main(String[] args) { public static int max(int num1, int num2) { int k = max(i, j); if (num1 > num2) int i = 5; int result; result = num1; int j = 2; System.out.println( else int k = max(i, j); if (num1 > num2) "The maximum between " + i + result = num2; result = num1; " and " + j + " is " + k); System.out.println( else } return result; "The maximum between " + i + result = num2; } " and " + j + " is " + k); } return result; } 13 16 Trace Method Invocation Trace Method Invocation j is now 2 declare variable result public static void main(String[] args) { public static int max(int num1, int num2) { public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int i = 5; int result; int j = 2; int j = 2; int k = max(i, j); if (num1 > num2) int k = max(i, j); if (num1 > num2) result = num1; result = num1; System.out.println( else System.out.println( else "The maximum between " + i + result = num2; "The maximum between " + i + result = num2; " and " + j + " is " + k); " and " + j + " is " + k); } } return result; return result; } } 14 17 Trace Method Invocation Trace Method Invocation invoke max(i, j) (num1 > num2) is true since num1 is 5 and num2 is 2 public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; int k = max(i, j); if (num1 > num2) public static void main(String[] args) { public static int max(int num1, int num2) { result = num1; int i = 5; int result; System.out.println( else int j = 2; "The maximum between " + i + result = num2; int k = max(i, j); if (num1 > num2) " and " + j + " is " + k); result = num1; } return result; System.out.println( else } "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; } 15 18
Trace Method Invocation Trace Method Invocation result is now 5 Execute the print statement public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; public static void main(String[] args) { public static int max(int num1, int num2) { int j = 2; int i = 5; int result; int k = max(i, j); if (num1 > num2) int j = 2; result = num1; int k = max(i, j); if (num1 > num2) System.out.println( else result = num1; "The maximum between " + i + result = num2; System.out.println( else " and " + j + " is " + k); "The maximum between " + i + result = num2; } return result; " and " + j + " is " + k); } } return result; } 19 22 Trace Method Invocation Call Stacks Each time a method is invoked, the system stores return result, which is 5 parameters and variables in an area of memory, known as a stack public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int result; int j = 2; When a method calls another method, the caller's stack int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else space is kept intact "The maximum between " + i + result = num2; " and " + j + " is " + k); } return result; New space is created to handle the new method call } When a method finishes, its associated space is released. 20 23 Trace Method Invocation Trace Call Stack return max(i, j) and assign the i is declared and initialized return value to k public static void main(String[] args) { public static void main(String[] args) { public static int max(int num1, int num2) { int i = 5; int i = 5; int result; int j = 2; int j = 2; int k = max(i, j); int k = max(i, j); if (num1 > num2) result = num1; System.out.println( else System.out.println( "The maximum between " + i + result = num2; "The maximum between " + i + " and " + j + " is " + k); " and " + j + " is " + k); } } return result; } public static int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } 21 24
Trace Call Stack Trace Call Stack j is declared and initialized pass the values of i and j to num1 and num2 public static void main(String[] args) { public static void main(String[] args) { int i = 5; int i = 5; int j = 2; int j = 2; int k = max(i, j); int k = max(i, j); System.out.println( System.out.println( "The maximum between " + i + "The maximum between " + i + " and " + j + " is " + k); " and " + j + " is " + k); } } public static int max(int num1, int num2) { public static int max(int num1, int num2) { int result; int result; if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else result = num2; result = num2; return result; return result; } } 25 28 Trace Call Stack Trace Call Stack Declare k pass the values of i and j to num1 and num2 public static void main(String[] args) { public static void main(String[] args) { int i = 5; int i = 5; int j = 2; int j = 2; int k = max(i, j); int k = max(i, j); System.out.println( System.out.println( "The maximum between " + i + "The maximum between " + i + " and " + j + " is " + k); " and " + j + " is " + k); } } public static int max(int num1, int num2) { public static int max(int num1, int num2) { int result; int result; if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else result = num2; result = num2; return result; return result; } } 26 29 Trace Call Stack Trace Call Stack Invoke max(i, j) (num1 > num2) is true public static void main(String[] args) { public static void main(String[] args) { int i = 5; int i = 5; int j = 2; int j = 2; int k = max(i, j); int k = max(i, j); System.out.println( System.out.println( "The maximum between " + i + "The maximum between " + i + " and " + j + " is " + k); " and " + j + " is " + k); } } public static int max(int num1, int num2) { public static int max(int num1, int num2) { int result; int result; if (num1 > num2) if (num1 > num2) result = num1; result = num1; else else result = num2; result = num2; return result; return result; } } 27 30
Recommend
More recommend