🧩 Methods · Beginner

Parameters vs arguments in Java

Formal parameters receive copies of argument values.

🧩 The mysteryYou call pair(last, first) and the method prints the names in the 'wrong' order. You named everything perfectly. Java just doesn't care about names.

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 arguments

Matched 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 1
🔮 Predict it

Your 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);
}
  1. Ana, Lee
  2. Lee, Ana
  3. Compile 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 42
🔮 Predict it

Your turn

What prints?

static void addTen(int n) {
    n = n + 10;
}
void main() {
    int pts = 5;
    addTen(pts);
    System.out.println(pts);
}
  1. 15
  2. 5
  3. 10
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.

⚠️ The trap

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 -> 12
💼 In the real world

In 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

  1. Parameter: placeholder in the definition; argument: value in the call
  2. Matched by position, left to right
  3. Arguments are evaluated left to right before the method starts
  4. Changing a parameter never changes the caller's variable
🤯 Did you know?

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);
}
  1. 2,1
  2. 1,2
  3. x,y
  4. 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);
}
  1. 0
  2. 42
  3. 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.

Next: the most argued question in Java. When you pass an *object*, does the method get the object itself?