3.8. Passing objects and primitives to methods

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

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

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:

  • It’s OK to define a method parameter with the same name as an instance variable (or object field). But this is not a recommended practice.
  • Within a method, a method parameter takes precedence over an object field. When the method modifyVal refers to the variable age, it refers to the method parameter age, not the instance variable age. To access the instance variable age within the method modifyVal, the variable name age needs to be prefixed with the keyword this (this is a keyword that refers to the object itself).

The keyword this is discussed in detail in chapter 6.

Exam Tip

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.

3.8.2. Passing object references to methods

There are two main cases:

  • When a method reassigns the object reference passed to it to another variable
  • When a method modifies the state of the object reference passed to it
When methods reassign the object references passed to them

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.

Figure 3.23. Objects of class Person, referred to by variables person1, person2, p1, and p2

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:

  • Person temp = p1: makes temp refer to the object referred to by p1
  • p1 = p: makes p1 refer to the object referred to by p2
  • p2 = temp: makes p2 refer to the object referred to by temp

These three steps are represented in figure 3.24.

Figure 3.24. The change in the objects referred to by variables during the execution of the method swap

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
When methods modify the state of the object references passed to them

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.

Figure 3.25. Modification of the state of an object passed to the method resetValueOfMember-Variable

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.103.96