Page 291 - Introduction to Programming with Java: A Problem Solving Approach
P. 291

                This shows that you can make your equals method return true when there is only approximate equal- ity, where you define “approximate” however you wish. We’ll discuss the equals method in more depth in Chapter 13.
7.5 Passing References as Arguments
By now, you should be fairly comfortable with the concept of passing an argument to a method. We’ve cov- ered all you need to know about passing primitive types as arguments. But you still need to know a bit more about passing references as arguments. In the example in Figure 7.4, we passed the nickCar reference as an argument to the equals method. The equals method assigned the nickCar reference to its otherCar parameter, and then it used the otherCar parameter to read the object’s data. In that example, we used a passed-in reference to read an object’s data. Now let’s use a passed-in reference to update an object’s data.
Suppose you pass a reference variable to a method, and inside the method you update the reference vari- able’s instance variables. What happens? Remember that a reference variable holds the address of an object, not the object itself. So in passing a reference variable argument to a method, a copy of the object’s address (not a copy of the object itself) is passed to the method and stored in the method’s parameter. Since the pa- rameter and the argument hold the same address value, they point to the same object. Thus, if the parameter’s instance variables are updated, then the update simultaneously updates the argument’s instance variables in the calling module. This is a case where aliasing (using two names for the same thing) is really handy.
Person-Swapping Example
gram. See the Person program in Figures 7.6 and 7.7. The Person program swaps names for two Person objects. As shown in Figure 7.6’s main method, the person1 reference variable starts with the name “Jon- athan” and the person2 reference variable starts with the name “Benji.” After the swapPerson method call, person1 has the name “Benji,” and person2 has the name “Jonathan.” The swapPerson method swaps the names by taking advantage of the phenomenon discussed above—if a reference variable is passed to a method, then the parameter and the argument refer to the same object, and an update to one means an update to the other as well. Bottom line: When you pass a reference to a method, you enable the method to modify the referenced object.
General-Purpose Swapping Algorithm
Before digging deeper into the Person program’s code, let’s come up with a general-purpose swapping algorithm. Having to swap two values is a very common programming require- ment, so you should make sure that you fully understand how to do it.
Suppose you’re asked to provide an algorithm that swaps the contents of two variables, x and y. To make the goal more concrete, you are given the following algorithm skeleton. Replace <Insert swap code here.>withappropriatepseudocodesothatthealgorithmprintsx=8, y=3.
x← 3
y← 8
<Insert swap code here.>
print "x = " + x + ", y = " + y
Note that the algorithm skeleton uses the formal version of pseudocode introduced near the end of Chap- ter 2. The formal-version pseudocode is more compact and closer to Java than the informal version. For
7.5 Passing References as Arguments 257
 Let’s see if you understand all of this reference-passing stuff by putting it in the context of a complete pro-
Apago PDF Enhancer
 How do you swap two values?
  
















































































   289   290   291   292   293