AP® Computer Science A · Unit 3 · Topic 3.5
Parameters, Arguments, and Pass by Value
Java always passes arguments by value: a method gets its own copy of whatever value you hand it. Changing that copy inside the method never touches the original variable back in the caller.
When you call a method with an argument, Java copies the value of that argument into the method's parameter variable. That parameter is a brand-new box in memory, separate from the argument's box, even though it starts out holding an equal value. The method can do whatever it wants to its own box, but it has no way to reach back and change the caller's box. That is why reassigning a parameter inside a method leaves the caller's original variable exactly as it was.
This matters most for primitives like int, double, and boolean, where the value itself is the number. It gets more interesting with arrays and objects, because what gets copied there is a reference, essentially an address pointing to the object in memory. The parameter and the argument are still separate variables, but now both copies point at the same object. So reassigning the parameter to a new array does not affect the caller, but changing an element inside the array that the reference points to does, because both variables are looking at the same object.
This distinction sets up everything else in building your own class: it explains why a constructor's parameters only initialize fields if you explicitly assign them, and why passing an object into a method lets that method mutate the object's state even though the reference itself was passed by value.
Key terms
- parameter
- The variable declared in a method's signature that receives a copied value when the method is called.
- argument
- The actual value or variable you supply at the call site when invoking a method.
- pass by value
- Java's rule that a method always receives a copy of the argument's value, never the original variable itself.
- reference
- A value that points to where an object or array lives in memory, itself passed by value like any other value.
Watch it explained
- How it worksHow does a parameter get a copy of the argument's value the moment a method is called?
- Worked exampleTrace int x = 5; doubleIt(x); where doubleIt sets its parameter to twice itself.
- Common mistakeWhy isn't the parameter the same variable as the argument that was passed in?
- Going furtherWhat changes when the argument is an array reference instead of a plain int?
Check yourself
End of topic.