JavaMethod Parameters

Java Method Parameters

Parameters vs Arguments

These two words are often used interchangeably, but they mean slightly different things. A parameter is the variable listed in a method's definition — a placeholder for a value the method expects to receive. An argument is the actual value you supply when you call the method.

Java
// "name" is the parameter
public static void greet(String name) {
    System.out.println("Hello, " + name);
}

public static void main(String[] args) {
    greet("Amina"); // "Amina" is the argument
}
Multiple Parameters

A method can accept several parameters, separated by commas. Arguments are matched to parameters by POSITION, not by name.

Java
public static void showProfile(String name, int age, String city) {
    System.out.println(name + " is " + age + " years old and lives in " + city + ".");
}

public static void main(String[] args) {
    showProfile("Diego", 29, "Lisbon");
}
Diego is 29 years old and lives in Lisbon.
Warning
The order of arguments must match the order of parameters. showProfile(29, "Diego", "Lisbon") would either fail to compile (if the types don't line up) or silently produce nonsense output — Java matches purely by position, never by parameter name.
Java is Always Pass-by-Value

This is one of the most misunderstood ideas in Java: EVERY argument in Java is passed by value, with no exceptions. When you call a method, Java copies the value of each argument into the method's parameter. The method works with that copy — it can never reach back and replace the caller's original variable.

Warning
For primitives (int, double, boolean, etc.), the copy is obviously just the value itself. But even for objects, Java is STILL pass-by-value — what gets copied is the REFERENCE (the "address") to the object, not the object itself. This means a method CAN reach through that reference and modify the fields of the object it points to, but it can NEVER make the caller's original variable point to a different object.
What CAN Be Changed: Mutating Fields

Java
class Counter {
    int value;
}

public static void increment(Counter c) {
    c.value = c.value + 1; // modifies the SAME object the caller has
}

public static void main(String[] args) {
    Counter counter = new Counter();
    counter.value = 10;

    increment(counter);
    System.out.println(counter.value);
}
11
Note
This works because counter and c both hold a copy of the SAME reference — they point at the exact same Counter object in memory. Changing c.value changes the object both variables are pointing to.
What CANNOT Be Changed: Reassigning the Reference

Java
class Counter {
    int value;
}

public static void replace(Counter c) {
    c = new Counter();  // c now points to a brand-new object
    c.value = 999;       // only affects the NEW object, local to this method
}

public static void main(String[] args) {
    Counter counter = new Counter();
    counter.value = 10;

    replace(counter);
    System.out.println(counter.value); // still 10!
}
10
Note
Inside replace, c = new Counter() only reassigns the LOCAL copy of the reference — it has no effect on the caller's counter variable, which still points to the original object. This is exactly why the value printed is still 10, not 999.
Same Idea With a String

Java
public static void tryToChange(String text) {
    text = "changed"; // only reassigns the local parameter
}

public static void main(String[] args) {
    String message = "original";
    tryToChange(message);
    System.out.println(message); // still "original"
}
original
Note
Strings are immutable and behave the same way any object reference does here — reassigning the parameter inside the method never touches the caller's variable.
Summary Table

What you do inside the method

Visible to the caller afterward?

Change a primitive parameter's value

No

Modify a field/element through an object/array reference parameter

Yes

Reassign an object reference parameter to point elsewhere

No

Tip
A simple way to remember it: you can change what an object CONTAINS through a passed-in reference, but you can never change WHICH object the caller's own variable refers to.
Practice Exercises
  1. Write a method that takes an int array and doubles every element in place — confirm the caller sees the change.

  2. Write a method that takes an int primitive and tries to double it — confirm the caller does NOT see any change.

  3. Write a method that takes a StringBuilder and appends text to it, then confirm the caller's StringBuilder reflects the change.