1 / 44

More with Methods (parameters, reference vs. value, array processing)

Understanding pass by value in Java: how it works, memory usage, and passing reference variables as parameters. Learn more about method declaration, call sequence, and variable types.

hkremer
Download Presentation

More with Methods (parameters, reference vs. value, array processing)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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: • 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. • 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. Call to swap method, passing two variables NOTE: data of formal parameters is being changed…but this does not affect actual parameters. Listing 5.3 p 136

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

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

  6. Review of Variables and Memory Usage • Two categories of variables • 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. • Two different locations of memory (more to come later) • FrameStack – contains local variables and parameters of methods • Heap – contains objects and arrays • The new keyword always creates an object or array and places it in the heap.

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

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

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

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

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

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

  13. Try this with the debugger!

  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: • An array • 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. 3) Assigning the array’s memory location to the reference variable • Declaring the reference variable 2) Creating the array on the heap main’s frame args 2 1 a 0 1 Frame Stack Heap

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

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

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

  20. 1 2 1 temp n1 n2 main’s frame swap’s frame args 2 1 a 0 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!! Frame Stack Heap

  21. array temp Swap First Two In Array’s frame main’s frame args 2 1 a 0 1 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. Frame Stack Heap

  22. array temp Swap First Two In Array’s frame main’s frame args 2 1 a 0 1 1 Frame Stack Heap

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

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

  25. Try this with the debugger!

  26. Searching for a 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.

  27. Linear Search of an Array • This class includes a method that receives two parameters: • An integer of the value you want to search for. • 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: • 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) • Some Elements of a Structure chart: • Boxes (for methods) • Connecting arrows (for invocations) See Liang pp 159-165

  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. PrintCalendar Case Study Let us use the PrintCalendar example to demonstrate the stepwise refinement approach.

  35. Design Diagram

  36. Design Diagram

  37. Design Diagram

  38. Design Diagram

  39. Design Diagram

  40. Design Diagram

  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. 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.

More Related