Pass-by-Value: The Copy Rule
Where you are: Week 0 review > Pass-by-value: the copy rule
Try This First
Predict the output before reading on:
public static void addFive(int n) { n = n + 5; }
int x = 10;
addFive(x);
System.out.println(x);
Reveal
10. Java copies x into a fresh parameter n. The method changes its own copy, not the caller’s x, so x is still 10 after the call.
Before You Start
Check each box you can do from memory. A box you cannot check yet is not a problem; it points you to a quick refresher, not a grade.
See The Method Header and How a Method Works if any of these feel unfamiliar.
Not sure? Take the 60-second self-check.
Try each from memory, then read the answer under it.
- What is a parameter, in one sentence? A named input variable a method receives when it is called.
- What does the return type
voidmean? The method hands nothing back; it does its work and returns.
What You Need To Walk In With
Walk into the next class able to state these:
- When you pass a primitive to a method, Java copies the value into a fresh parameter. The parameter and the caller’s variable are separate.
- Assigning to a primitive parameter never changes the caller’s variable. To hand back a new value,
returnit. - A helper cannot swap two of the caller’s primitives, because it only swaps its own copies.
- For an object, the reference is copied, so the method points at the same object and can change its fields (covered in depth in the next course).
You should be able to: trace a primitive parameter call and prove the caller’s variable is unchanged.
How It Works
First time seeing "primitive"? Open for a 20-second refresher.
A primitive is one of Java’s eight built-in value types: int, double, boolean, char, long, float, short, and byte. Unlike objects, a primitive variable holds its value directly, not a reference to it. See Variables and the Primitive Types for the full picture.
Calling a method with a primitive argument copies the value into a new parameter variable. The two live in separate memory, so assigning to the parameter does not touch the caller’s variable.
public static void addFive(int n) {
n = n + 5; // changes n, not the caller's variable
}
int x = 10;
addFive(x);
System.out.println(x); // prints 10
| Step | x in caller |
n in method |
|---|---|---|
| Before the call | 10 | does not exist yet |
Copy x into n |
10 | 10 |
n = n + 5 |
10 | 15 |
| Method returns | 10 | gone |
To produce a new value, return it and let the caller assign the result:
public static int addFive(int n) {
return n + 5;
}
int x = 10;
x = addFive(x); // now x is 15
Worked Example: Predict, Then Check
public static void tryToSwap(int a, int b) {
int t = a; a = b; b = t;
}
int p = 1, q = 2;
tryToSwap(p, q);
System.out.println(p + " " + q);
Predict the output before reading on.
Reveal
1 2. The method swaps its own copies a and b, then those copies vanish. The caller’s p and q never change. A helper cannot swap two caller primitives; do the swap at the call site instead.
A Common Mistake
A frequent belief is that a parameter is the same variable as the argument, so changing the parameter changes the caller’s variable. It does not, for primitives: the parameter is a copy. Drawing two columns (one for the caller’s variable, one for the parameter) makes the independence visible from the moment of the call. (Source: BJP (Reges and Stepp), Ch 3; JLS 25, §15.12.4.4.)
Go Deeper (optional)
For the curious: Java copies the value of whatever you pass. For a primitive, that value is the number itself, so the method gets its own number. For an object, that value is a reference (an arrow to the object), so the copy still points at the same object, and changing the object’s fields is visible to the caller. The rule is one rule (copy the value), and the two behaviors follow from what the value is.
Check Yourself
Close the notes and answer each one from memory, then reveal it. Pulling an idea back from memory is one of the strongest ways to make it stick.
Check your understanding
addFive(int n){ n=n+5; } int x=10; addFive(x); print(x). What prints?
A temp-based swap helper leaves the caller’s two ints unchanged. Why?
How does a primitive method hand a new value back to the caller?
A method changes a field of an object reference it received. Does the caller see it?