The effect of passing arguments to a method differs depending on whether you are passing a value of primitive type (such as 5 or true) or a value of reference type (such as “Hello” or game1).
Subsection3.5.1Primitive Types: Pass by Value
When an argument of primitive type is passed to a method, a copy of the argument is passed to the formal parameter. For example, consider the PrimitiveCall class shown in Figure 3.5.1. Note that we have an int variable k, which initially stores the value 5, and a method myMethod(), which takes an int parameter n. In this case, when we invoke myMethod(k), k’s value (5) is copied into n and stored there during the method. Run the code below to trace through it. Notice that the original variable in main does not change its value because its value is copied into the parameter variable.
Activity3.5.1.
Run the code below. Click on Show CodeLens to step through the code with the Next button.
One implication of passing a copy of a primitive value to a method is that the original value of k in main() cannot be altered from inside the method. Thus, the output generated by PrimitiveCall is
Note that in main(), k’s value is printed both before and after myMethod() is called, but that its value remains unaffected even though n’s value is changed within the method. This is because myMethod() contains just a copy of k’s value, not k itself. Any changes to the copy within myMethod() leave k unaltered.
Principle3.5.2.Passing a Primitive Value.
When a value of a primitive type, like boolean or int or double, is passed to a method, a copy of the value is passed. That’s why its original value remains unchanged outside the method, even if the copy is changed inside the method.
Subsection3.5.2Passing Reference Types
In contrast to this, when an argument of a reference type, like String or another class type like OneRowNim, is passed to a method, a copy of the reference to the object itself is assigned to the parameter. For example, in the case of a String parameter or a OneRowNim parameter, the method would be given a reference to the object–that is, the address of the object. The object itself is not passed, because it would be too inefficient to copy the entire object with all its data and methods. However, because the object’s reference gives the object’s location in memory, the method will have access to the object and can make changes to the original object from within the method.
For example, consider the ReferenceCall class (Listing 3.5.4). In this case, myMethod() takes a parameter g of type OneRowNim. Because a OneRowNim instance is an object, g is a reference variable. So when myMethod(game) is invoked in main(), a reference to game is passed to myMethod(). Note that in myMethod(), we use takeSticks(3) to change the number of sticks of g from 10 to 7 and that this change persists even after the method returns control to main(). The reason is that during the method’s execution, both game and g refer to the exact same object.
Activity3.5.2.
Run the code below. Click on Show CodeLens to step through the code with the Next button.
The output generated by ReferenceCall would be
main: Number of sticks: 10
myMethod: Number of sticks: 10
myMethod: Number of sticks: 7
main: Number of sticks: 7
This illustrates that when passing a reference variable to a method, it is possible for the method to change the state of the object associated with the reference variable. In subsequent chapters, we will see ways to make use of this feature of reference parameters.
Principle3.5.6.Passing a Reference.
When a reference to an object is passed to a method, any changes made to the object from within the method will persist when the method is finished executing.
Principle3.5.7.DEBUGGING TIP: Side Effects.
An unintended change to an object is called a side effect. Care should be taken in designing methods that the method does not produce unwanted side effects in objects passed as reference parameters.