Parameters vs arguments in Java
Formal parameters receive copies of argument values.
Placeholders vs values
Parameters are the variables named in the method's header. Arguments are the actual values you pass in a call. Arguments flow in through parameters; the return value flows back out.
static int area(int w, int h) { // parameters
return w * h;
}
area(3, 4); // 3 and 4 are argumentsMatched by position
Arguments are matched to parameters by position, left to right: first argument to first parameter, second to second. The names of the caller's variables don't matter at all.
static void show(int x, int y) {
System.out.println(x + "," + y);
}
int y = 1, x = 2;
show(y, x); // method's x gets 1Your turn
What prints?
static void pair(String first, String last) {
System.out.println(last + ", " + first);
}
void main() {
String last = "Ana";
String first = "Lee";
pair(last, first);
}Ana, LeeLee, AnaCompile error
Show the answer
main's last ("Ana") is the first argument, so it lands in the parameter **first**. Then the method prints last + ", " + first: Lee, Ana. Position wins over names.
Fresh copies
When a call starts, each parameter becomes a brand-new local variable holding a copy of its argument's value. The method can change its copy all it likes; the caller's variable is never touched.
static void reset(int n) {
n = 0; // changes only the copy
}
int score = 42;
reset(score);
// score is still 42Your turn
What prints?
static void addTen(int n) {
n = n + 10;
}
void main() {
int pts = 5;
addTen(pts);
System.out.println(pts);
}15510
Show the answer
5. n started as a copy of 5 and became 15, but that copy vanished when the method returned. pts was never involved.
Left to right, before the call
All arguments are evaluated left to right, before the method starts. With combine(i++, i): i++ yields 1 and makes i 2, *then* the second argument is read as 2. So a = 1, b = 2. Side effects inside arguments are legal but confusing; avoid them.
static int combine(int a, int b) {
return a * 10 + b;
}
int i = 1;
combine(i++, i); // a=1, b=2 -> 12In real projects
Swapped arguments of the same type are a sneaky production bug: transfer(to, from, amount) compiles fine and sends money the wrong way. Teams fight this with clear names, IDE parameter hints, and small types like AccountId so the compiler can catch mix-ups.
Key takeaways
- Parameter: placeholder in the definition; argument: value in the call
- Matched by position, left to right
- Arguments are evaluated left to right before the method starts
- Changing a parameter never changes the caller's variable
Java guarantees left-to-right evaluation of arguments. In C, the order is unspecified, and something like f(i++, i) is even undefined behavior: different compilers may give different results.
Practice questions
What does this print?
static void show(int x, int y) {
System.out.println(x + "," + y);
}
void main() {
int y = 1, x = 2;
show(y, x);
}- 2,1
- 1,2
- x,y
- Compile error
Check your answer
1,2. The first argument (main's y, which is 1) goes into the first parameter x. Names in the caller don't matter.
What does this print?
static void reset(int n) {
n = 0;
}
void main() {
int score = 42;
reset(score);
System.out.println(score);
}- 0
- 42
- Compile error
Check your answer
42. n is a separate variable that started as a copy of 42. Setting n to 0 doesn't affect score.