Sub topic 6
Trace Primitive Values and Object References
Java uses pass-by-value when calling a method. This means each parameter receives a copy of the corresponding argument’s value. A parameter is a variable in a method’s declaration. An argument is a value given to the method when it is called.
What gets copied depends on the value:
- A primitive is a simple value, such as an
int. Java copies the value itself. - An object can hold data and have methods. An object reference is a value that points to an object. Java copies the reference, not the object.
This difference matters when a method changes something. A change to a copied primitive does not change the caller’s variable. But two references can point to the same object, so a change to that object can be visible to both.
Try an example
Save this code as Counter.java. A field is a variable that belongs to an object. The value field starts at 0 because Java gives an int field that default value.
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 from the folder containing the file:
javac Counter.java
java CounterThe program prints 1 and then 1. Let’s trace why.
Follow the copies
In main, number holds the primitive value 1. The counter variable holds a reference to a Counter object. Call that object A.
When change(number, counter) runs, Java copies both argument values into the parameters. The parameters have the same names as the variables in main, but they are separate variables.
| Point in the method | `number` in `main` | `number` in `change` | `counter` in `change` | A’s `value` |
|---|---|---|---|---|
| Just after the call | 1 | 1 | Points to A | 0 |
| After `number++` | 1 | 2 | Points to A | 0 |
| After `counter.increment()` | 1 | 2 | Points to A | 1 |
| After `counter = new Counter()` | 1 | 2 | Points to a new object | 1 |
The first statement, number++, changes only the parameter’s copy. The variable in main still holds 1.
Next, counter.increment() uses the copied reference to reach object A. The increment method changes A’s value to 1. The reference in main also points to A, so main can see that change.
Finally, counter = new Counter() makes the parameter point to a new object. This is reassignment: giving a variable a different value. It does not change the reference held by counter in main, which still points to A.
When change finishes, its parameters are no longer available. The two print statements in main therefore see number as 1 and A’s value as 1.
Check your understanding
Exercise
In change, swap the order of counter.increment() and counter = new Counter(). What does the program print?
Show answer
It prints 1 and then 0. The parameter first points to a new counter, so increment() changes that new object. The counter in main still points to A, whose value remains 0.
Exercise
In the original program, what happens to number in main if change sets its number parameter to 10?
Show answer
It stays 1. The parameter holds a copy of the primitive value, so changing the parameter does not change the variable in main.