Sub topic 6
Trace Primitive Values and Object References
When you call a Java method, Java gives each parameter a copy of its argument’s value. This rule is called pass by value.
A parameter is a variable named in a method’s declaration. An argument is the value supplied when the method is called. The method that makes the call is the caller.
The effect of a copy depends on what the value contains:
- A primitive value is a simple value, such as an integer or a boolean. Copying it gives the method a separate value to work with.
- A reference is a value that identifies an object. An object is a piece of data created from a class. Copying a reference does not copy the object.
This difference matters when a method changes data. A method cannot directly replace the caller’s variable by changing its parameter. But it can use a copied reference to change an object that the caller can also use.
Trace a Number and an Object
Save this program as Counter.java:
class Counter {
int value;
void increment() {
value++;
}
static void change(int number, Counter counter) {
number++;
counter.increment();
counter = new Counter();
}
public static void main(String[] args) {
int number = 1;
Counter counter = new Counter();
change(number, counter);
System.out.println(number);
System.out.println(counter.value);
}
}Compile and run it:
javac Counter.java
java CounterThe output is:
1
1The first line is the number in main. The second is the value stored in the original Counter object. Let’s trace why both are 1.
Before the call
In main, the variable number holds the primitive value 1.
The variable counter holds a reference to a Counter object. That object has a field: a variable stored in the object. Its field, value, starts at 0. Java gives an int field the default value 0 when a new object is created.
For the trace, call this object A. The letter is just a name for the object; it is not part of the program.
At the call
The call change(number, counter) copies both argument values into the parameters of change.
- The parameter
numbergets its own copy of the value 1. - The parameter
countergets a copy of the reference to object A.
The caller and the method have separate variables, even though the variables have the same names. Both counter variables now refer to the same object.
Inside the method
The method runs these three statements:
number++changes the parameter’s copy from 1 to 2. The caller’snumberstays 1.counter.increment()uses the copied reference to reach object A. Theincrementmethod changes A’svaluefield from 0 to 1.counter = new Counter()creates a second object, B, and puts its reference in the parameter. This is reassignment: giving an existing variable a different value. The caller’scounterstill refers to A.
The table shows the state after each step. “Parameter counter” means the variable inside change; “caller counter” means the variable inside main.
| Point in the program | Caller number | Parameter number | Caller counter | Parameter counter | Object A value |
|---|---|---|---|---|---|
| Just after the call | 1 | 1 | A | A | 0 |
| After `number++` | 1 | 2 | A | A | 0 |
| After `counter.increment()` | 1 | 2 | A | A | 1 |
| After parameter reassignment | 1 | 2 | A | B | 1 |
Here is the reference state just before change finishes. Object B is new, so its value field is still 0.
When change finishes, its parameters are no longer available. The caller’s number is still 1, and its counter still refers to A. Since A’s field was changed, counter.value is 1.
Changing an Object Versus Reassigning a Variable
These actions can look similar, but they affect different things:
- Changing a field, as
increment()does, changes the object. Anyone with a reference to that same object can see the changed field. - Reassigning a reference variable changes which object that variable refers to. It does not change other reference variables.
Changing an object’s contents is also called mutation. In the example, incrementing A’s field is a mutation. Assigning B’s reference to the parameter is not a mutation of A.
It is useful to ask two questions when tracing a method:
- Is the statement changing a variable, or is it changing an object?
- If it changes an object, do the caller and method refer to that same object?
Do not conclude that an object argument is “passed by reference.” That phrase can suggest that the method can replace the caller’s variable. In Java, the reference itself is copied.
Arrays Follow the Same Rule
An array is an object that holds a fixed number of values. An array variable holds a reference, so a method can change an array’s contents through a copied reference.
Save this program as ArrayDemo.java:
class ArrayDemo {
static void edit(int[] values) {
values[0] = 7;
values = new int[] {9};
}
public static void main(String[] args) {
int[] values = {1, 2};
edit(values);
System.out.println(values[0]);
}
}Run it with:
javac ArrayDemo.java
java ArrayDemoIt prints:
7At the call, the parameter receives a copy of the reference to the caller’s array. The statement values[0] = 7 changes the first value in that shared array.
The next statement creates a different array and assigns its reference to the parameter. It does not replace the caller’s array. The caller therefore still sees 7 as the first value of its original array.
Strings and null
A String is also an object, and a reference to one is passed by value. A String is immutable: once created, its contents cannot be changed. A method can make a different string and assign its reference to a parameter, but that does not replace the caller’s string variable.
A reference can also be null, meaning it refers to no object. If a method receives null, its parameter gets a copy of null. Trying to use it to access a field or call an instance method causes a NullPointerException. This is an error that signals an attempt to use null as an object reference.
Setting a parameter to null does not set the caller’s variable to null. It only changes the parameter’s copy.
Give a New Value Back to the Caller
What if a method should produce a new number for the caller? It can return the result, and the caller can choose to assign that result to its own variable.
Save this program as ReturnDemo.java:
class ReturnDemo {
static int addOne(int number) {
return number + 1;
}
public static void main(String[] args) {
int number = 1;
number = addOne(number);
System.out.println(number);
}
}Compile and run it:
javac ReturnDemo.java
java ReturnDemoIt prints 2. The method does not change the caller’s variable through its parameter. Instead, it returns 2, and the assignment in main changes the caller’s variable.
The same idea works for references: a method can return a reference to an object, and the caller can assign that reference to its variable.
Practice the Trace
Exercise
A caller passes an int variable holding 4 to a method. The method adds 3 to its parameter but returns nothing. What does the caller’s variable hold afterward?
Show answer
It still holds 4. The method changed its own copy of the primitive value.
Exercise
A caller passes a reference to a Counter whose value is 2. The method sets the object’s value to 5, then assigns a new Counter to its parameter. What value does the caller see in its original Counter?
Show answer
The caller sees 5. The first change affected the shared object; the reassignment changed only the parameter.
Exercise
A method receives an array reference and changes its first element to 8. The caller then reads the first element of the same array. What does it see?
Show answer
It sees 8. The copied reference and the caller’s reference identify the same array.