Presentation is loading. Please wait.

Presentation is loading. Please wait.

More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6.

Similar presentations


Presentation on theme: "More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6."— Presentation transcript:

1 More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6

2 Pass by Value Parameter passing can be of two types: 1.Pass by Value A COPY of the actual parameter’s contents is sent to the method and inserted into the formal parameter. Changing the contents of the formal parameter does not affect the actual parameter. 2.Pass by Reference The formal parameter IS THE SAME AS the actual parameter. Changing the contents of the formal parameter also changes the actual parameter. Java is strictly a pass-by-value language. The contents of variable passed to a method will not change based on actions done within the method. HOWEVER, some variables are themselves REFERENCES. The objects that they reference can be changed if a called method accesses these objects through the passed in parameters. (More about this later).

3 NOTE: data of formal parameters is being changed…but this does not affect actual parameters. Call to swap method, passing two variables Listing 5.3 p 136

4 Anatomy of Method Declaration and Call return type identifier formal parameters method body No return statement needed for void method specify identifier Pass actual parameters (arguments) No use of return value because return type is void method declaration method call modifiers

5 Processing Sequence of a Method Call method call method declaration 2) Pass by value: n1 is a copy of num1, n2 is a copy of num2. 3) Method body executes. 4) After method terminates, control returns to right after the calling statement. 1) invoke the method

6 Review of Variables and Memory Usage Two categories of variables Two categories of variables Value – contains actual data (integer, float, double, boolean, or other primitive data type) Value – contains actual data (integer, float, double, boolean, or other primitive data type) Reference – contains a reference (pointer) to an object or array that is located on the heap. Reference – contains a reference (pointer) to an object or array that is located on the heap. Two different locations of memory (more to come later) Two different locations of memory (more to come later) Frame Stack – contains local variables and parameters of methods Frame Stack – contains local variables and parameters of methods Heap – contains objects and arrays Heap – contains objects and arrays The new keyword always creates an object or array and places it in the heap. The new keyword always creates an object or array and places it in the heap.

7 Frame Stack args num1 1 num2 main’s frame 2 In main(), before calling swap()

8 Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 1 n2 2 temp In swap(), before assigning n1 to temp

9 Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 1 n2 2 temp 1 In swap(), before assigning n2 to n1

10 Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 2 n2 2 temp 1 In swap(), before assigning temp to n2

11 Frame Stack args num1 1 num2 main’s frame 2 swap’s frame n1 2 n2 1 temp 1 In swap(), after assigning temp to n2 Note: the formal parameter values changed, but not the actual parameters

12 Frame Stack args num1 1 num2 main’s frame 2 Back in main(), after swap() returns

13

14 Passing Reference Variables as Parameters As the previous example shows, Java is strictly a pass-by-value language. The contents of variable passed to a method will not change based on actions done within the method. HOWEVER, some variables are themselves REFERENCES. The objects that they reference can be changed if a called method accesses these objects through the passed in parameters. Any variable that is not a primitive data type (int, boolean, float, double, etc.) is a REFERENCE variable. That is, the variable is a pointer, pointing to the object. This could be: 1)An array 2)An instance of a class The next example shows the effects of passing an array as a parameter to a method.

15 Example 6.3 p180 – TestPassArray Class

16 Heap args Frame Stack main’s frame a 1)Declaring the reference variable 01 1 2 2) Creating the array on the heap 3) Assigning the array’s memory location to the reference variable

17 Heap args a Frame Stack main’s frame 01 1 2 swap’s frame n1 1 n2 2 temp In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

18 Heap args a Frame Stack main’s frame 01 1 2 swap’s frame n1 1 n2 2 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

19 Heap args a Frame Stack main’s frame 01 1 2 swap’s frame n1 2 n2 2 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed.

20 Heap args a Frame Stack main’s frame 01 1 2 swap’s frame n1 2 n2 1 temp 1 In this case, individual array elements are passed. The array elements are primitive data types, so copies of the data are passed. Original data does not change!!

21 In this case, the array variable itself is passed. The array variable is a reference type of variable, so a copy of the reference is passed. Heap args a Frame Stack main’s frame 01 1 2 Swap First Two In Array’s frame arraytemp

22 Heap args a Frame Stack main’s frame 01 1 2 Swap First Two In Array’s frame arraytemp 1

23 Note: data in the array itself is changed Heap args a Frame Stack main’s frame 01 2 2 Swap First Two In Array’s frame arraytemp 1

24 Heap args a Frame Stack main’s frame 01 2 1 Swap First Two In Array’s frame arraytemp 1 Original data did change!!! Note: data in the array itself is changed

25

26 Searching for a Value in an Array It is very common to need to find a particular value in an array It is very common to need to find a particular value in an array The following example shows a simple approach for doing this…it is called a Linear Search. The following example shows a simple approach for doing this…it is called a Linear Search.

27 Linear Search of an Array This class includes a method that receives two parameters: 1)An integer of the value you want to search for. 2)An integer array that you want to search in. The method will search the array, looking for the desired value. If it finds the value, the method returns the index of the element containing the value. If the value is not found, the method returns -1.

28 Method Abstraction Separating method use from its implementation The signature is the only thing the caller of a method needs to know. The details of the implementation should not affect the caller. Remember: Signature = modifier + return type + name + formal parameter list

29 To Illustrate Method Abstraction… Consider the Java Online Documentation: Consider the Java Online Documentation: http://java.sun.com/j2se/1.5.0/docs/api/index.html http://java.sun.com/j2se/1.5.0/docs/api/index.html http://java.sun.com/j2se/1.5.0/docs/api/index.html http://java.sun.com/j2se/1.5.0/docs/api/index.html

30 Consider the description of the Math.round() method…all you see is the signature. This is an abstraction. You don’t need to know how it implements the code; these details are hidden from the user of the method.

31 The documentation will provide information about what is returned, the formal parameters expected, and any modifiers. This is all the caller of a method needs.

32 Top-Down Design and Stepwise Refinement Structure Chart: top-down breakdown of tasks (methods) with data couples (parameters and return values) Structure Chart: top-down breakdown of tasks (methods) with data couples (parameters and return values) Some Elements of a Structure chart: Some Elements of a Structure chart: Boxes (for methods) Boxes (for methods) Connecting arrows (for invocations) Connecting arrows (for invocations) See Liang pp 159-165

33 33 Stepwise Refinement (Optional) The concept of method abstraction can be applied to the process of developing programs. When writing a large program, you can use the “divide and conquer” strategy, also known as stepwise refinement, to decompose it into subproblems. The subproblems can be further decomposed into smaller, more manageable problems.

34 34 PrintCalendar Case Study Let us use the PrintCalendar example to demonstrate the stepwise refinement approach.

35 35 Design Diagram

36 36 Design Diagram

37 37 Design Diagram

38 38 Design Diagram

39 39 Design Diagram

40

41 41 Implementation: Top-Down Top-down approach is to implement one method in the structure chart at a time from the top to the bottom. Stubs can be used for the methods waiting to be implemented. A stub is a simple but incomplete version of a method. The use of stubs enables you to test invoking the method from a caller. Implement the main method first and then use a stub for the printMonth method. For example, let printMonth display the year and the month in the stub. Thus, your program may begin like this:

42

43

44 44 Implementation: Bottom-Up Bottom-up approach is to implement one method in the structure chart at a time from the bottom to the top. For each method implemented, write a test program to test it. Both top-down and bottom-up methods are fine. Both approaches implement the methods incrementally and help to isolate programming errors and makes debugging easy. Sometimes, they can be used together.


Download ppt "More with Methods (parameters, reference vs. value, array processing) Corresponds with Chapters 5 and 6."

Similar presentations


Ads by Google