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.
// "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.
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.
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.
What CAN Be Changed: Mutating Fields
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
What CANNOT Be Changed: Reassigning the Reference
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
Same Idea With a String
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
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 |
Practice Exercises
Write a method that takes an int array and doubles every element in place — confirm the caller sees the change.
Write a method that takes an int primitive and tries to double it — confirm the caller does NOT see any change.
Write a method that takes a StringBuilder and appends text to it, then confirm the caller's StringBuilder reflects the change.