[6.6] Determine the effect upon object references and primitive values when they are passed into methods that change the values.
In this section, you’ll learn the difference between passing object references and primitives to a method. You’ll determine the effect on object references and primitive values when they’re passed into methods that change the values.
Object references and primitives behave in a different manner when they’re passed to a method because of the differences in how these two data types are internally stored by Java. Let’s start with passing primitives to methods.
The value of a primitive data type is copied and passed to a method. Hence, the variable whose value was copied doesn’t change:
The output of the preceding code is as follows:
0 1 0
In the preceding code, method modifyVal seems to accept and modify the argument passed to it. This book includes such code because you might see similar code on the exam, which doesn’t follow coding or naming conventions. But please follow the coding conventions when you write code on real projects.
The method modifyVal accepts a method argument a of type int. In this method, the variable a is a method parameter and holds a copy of the value that’s passed to it. The method increments the value of the method parameter a and prints its value.
When the class Office calls the method modifyVal , it passes a copy of the value of the object field age to it. The method modifyVal never accesses the object field age. Hence, after the execution of this method, the value of the method field age prints as 0 again.
What happens if the definition of the class Employee is modified as follows (modifications in bold):
class Employee { int age; void modifyVal(int age) { age = age + 1; System.out.println(age); } }
The class Office will still print the same answer because the method modifyVal defines a method parameter with the name age (do you remember the topic on variable scopes covered earlier in this chapter?). Note the following important points related to passing a method parameter to a method:
The keyword this is discussed in detail in chapter 6.
When you pass a primitive variable to a method, its value remains the same after the execution of the method. The value doesn’t change, regardless of whether the method reassigns the primitive to another variable or modifies it.
There are two main cases:
When you pass an object reference to a method, the method can assign it to another variable. In this case, the state of the object, which was passed on to the method, remains intact. When a method is passed a reference value, a copy of the reference (that is, the memory address) is passed to the invoked method. The callee can do whatever it wants with its copy without ever altering the original reference held by the caller.
The following code example explains this concept. Suppose you have the following definition of the class Person:
class Person { private String name; Person(String newName) { name = newName; } public String getName() { return name; } public void setName(String val) { name = val; } }
What do you think is the output of the following code?
In the preceding code, creates two object references, person1 and person2, illustrated in step 1 of figure 3.23. The boxed values represent objects of the class Person. prints John:Paul—the value of person1.name and person2.name.
The code then calls the method swap and passes to it the objects referred to by person1 and person2. When these objects are passed as arguments to the method swap, the method arguments p1 and p2 also refer to these objects. This behavior is illustrated in step 2 in figure 3.23.
The method swap defines three lines of code:
These three steps are represented in figure 3.24.
As you can see in figure 3.24, the reference variables person1 and person2 are still referring to the objects that they passed to the method swap. Because no change was made to the values of the objects referred to by variables person1 and person2, line from the previous page prints John:Paul again.
The output of the preceding code is as follows:
John:Paul John:Paul
Let’s see how a method can change the state of an object so that the modified state is accessible in the calling method. Assume the same definition of the class Person, listed again for your convenience:
class Person { private String name; Person(String newName) { name = newName; } public String getName() { return name; } public void setName(String val) { name = val; } }
What’s the output of the following code?
The output of the preceding code is as follows:
John Rodrigue
The method resetValueOfMemberVariable accepts the object referred to by person1 and assigns it to the method parameter p1. Now both variables, person1 and p1, refer to the same object. p1.setName("Rodrigue") modifies the value of the object referred to by the variable p1. Because the variable person1 also refers to the same object, person1.getName() returns the new name, Rodrigue, in the method main. This sequence of actions is represented in figure 3.25.
18.119.103.96