Let us review the computer science terms that describe how parameters can be passed to a method (or a function) in a programming language. The term call by value means that the method gets just the value that the caller provides. In contrast, call by reference means that the method gets the location of the variable that the caller provides. Thus, a method can modify the value stored in a variable passed by reference but not in one passed by value. These “call by . . .” terms are standard computer science terminology describing the behavior of method parameters in various programming languages, not just Java. (There is also a call by name that is mainly of historical interest, being employed in the Algol programming language, one of the oldest high-level languages.)
The Java programming language always uses call by value. That means that the method gets a copy of all parameter values. In particular, the method cannot modify the contents of any parameter variables passed to it.
For example, consider the following call:
double percent = 10;
No matter how the method is implemented, we know that after the method call, the value of percent is still 10.
Let us look a little more closely at this situation. Suppose a method tried to triple the value of a method parameter:
public static void tripleValue(double x) // doesn’t work
x = 3 * x;
Let’s call this method:
double percent = 10;
However, this does not work. After the method call, the value of percent is still 10. Here is what happens:
- x is initialized with a copy of the value of percent (that is, 10).
- x is tripled—it is now 30. But percent is still 10 (see Figure 4.6).
- The method ends, and the parameter variable x is no longer in use.
There are, however, two kinds of method parameters:
- Primitive types (numbers, boolean values)
- Object references
You have seen that it is impossible for a method to change a primitive type parameter. The situation is different for object parameters. You can easily implement a method that triples the salary of an employee:
public static void tripleSalary(Employee x) // works
When you call
harry = new Employee(. . .);
Figure 4.6 Modifying a numeric parameter has no lasting effect.
then the following happens:
- x is initialized with a copy of the value of harry—that is, an object reference.
- The raiseSatary method is applied to that object reference. The Employee object to which both x and harry refer gets its salary raised by 200 percent.
- The method ends, and the parameter variable x is no longer in use. Of course, the object variable harry continues to refer to the object whose salary was tripled (see Figure 4.7).
As you have seen, it is easily possible—and in fact very common—to implement methods that change the state of an object parameter. The reason is simple. The method gets a copy of the object reference, and both the original and the copy refer to the same object.
Many programming languages (in particular, C++ and Pascal) have two mechanisms for parameter passing: call by value and call by reference. Some programmers (and unfortunately even some book authors) claim that Java uses call by reference for objects. That is false. As this is such a common misunderstanding, it is worth examining a counterexample in detail.
Let’s try to write a method that swaps two Employee objects:
public static void swap(Emptoyee x, Employee y) // doesn’t work
Employee temp = x;
x = y;
y = temp;
If Java used call by reference for objects, this method would work:
var a = new Employee(“Alice”, . . .);
var b = new Employee(“Bob”, . . .);
// does a now refer to Bob, b to Alice?
However, the method does not actually change the object references that are stored in the variables a and b. The x and y parameters of the swap method are initialized with copies of these references. The method then proceeds to swap these copies.
// x refers to Alice,
y to Bob Employee temp = x;
x = y;
y = temp;
// now x refers to Bob, y to Alice
But ultimately, this is a wasted effort. When the method ends, the parameter variables x and y are abandoned. The original variables a and b still refer to the same objects as they did before the method call (see Figure 4.8).
This demonstrates that the Java programming language does not use call by reference for objects. Instead, object references are passed by value.
Here is a summary of what you can and cannot do with method parameters in Java:
- A method cannot modify a parameter of a primitive type (that is, numbers or boolean values).
- A method can change the state of an object parameter.
- A method cannot make an object parameter refer to a new object.
The program in Listing 4.4 demonstrates these facts. The program first tries to triple the value of a number parameter and does not succeed:
End of method: x=30.0
It then successfully triples the salary of an employee:
End of method: salary=150000.0
After the method, the state of the object to which harry refers has changed. This is possible because the method modified the state through a copy of the object reference.
Finally, the program demonstrates the failure of the swap method:
End of method: x=Bob
End of method: y=Alice
As you can see, the parameter variables x and y are swapped, but the variables a and b are not affected.
Source: Horstmann Cay S. (2019), Core Java. Volume I – Fundamentals, Pearson; 11th edition.