Parameters versus arguments
A method that always does the same thing is limited. sayHello() can only print one message. To make a method useful in many situations, give it inputs. Two words describe those inputs, and people often mix them up:
- A parameter is the variable declared in the method's definition, between the parentheses. It is a placeholder that says "this method needs a value of this type here".
- An argument is the actual value you supply when you call the method.
In short, parameters belong to the definition and arguments belong to the call. That is the answer to the common question "argument versus parameter in Java": same slot, seen from two sides.
void sayHello(String audience) { // audience is a parameter
System.out.println("Hello " + audience);
}
sayHello("humans"); // "humans" is an argument
The call prints Hello humans. Inside the method, audience behaves like an ordinary local variable that starts out holding the argument's value. Each call can pass a different argument, so sayHello("cats") prints Hello cats from the same code.
A parameter declaration has a type and a name, as in String audience. Java checks types at compile time, so only a String can be passed here.
Multiple parameters
List several parameters separated by commas. Each one needs its own type, even when the types are the same:
void sayHello(String name, int age) {
System.out.println(name + ", aged " + age);
}
sayHello("John", 30); // John, aged 30
sayHello("Mary", 26); // Mary, aged 26
Arguments are matched to parameters by position. The first argument goes to the first parameter, the second to the second, and so on. Java has no named arguments, so the order is part of the method's contract. Swapping the two arguments is a compile error, not a silent bug: sayHello(26, "Mary") is rejected with argument mismatch; int cannot be converted to String.
The number of arguments must also match. Calling sayHello("Mary") with one argument gives method sayHello ... cannot be applied to given types, followed by required: String,int, found: String and reason: actual and formal argument lists differ in length. The message lists what the method expects, which is usually enough to fix the call.
Passing variables and expressions
An argument can be a literal, a variable or any expression with the right type:
String name = "Andy";
int age = 28;
sayHello(name, age); // Andy, aged 28
sayHello("Mary", age); // Mary, aged 28
sayHello("Tom", age + 1); // Tom, aged 29
The method receives the value of the expression. A variable in the call and the parameter in the method are two different variables, even when they share a name, like name above. Each lives in its own scope.
The exercise: welcome a user
The lesson asks for a method welcomeUser and two calls to it.
- Define the method. It is
void, meaning it returns nothing and only prints. It has oneStringparameter namedname. - Print the greeting. Join
"Welcome, ",nameand"!"with+. Mind the space after the comma, so that"Andy"givesWelcome, Andy!. - Call it with a literal. In
mainFunction,welcomeUser("Andy")passes the text directly. - Create a variable.
String name = "John";insidemainFunction. Thisnameis separate from the parameter of the same name. - Call it with the variable.
welcomeUser(name)passes the value stored in the variable,"John".
void welcomeUser(String name) {
System.out.println("Welcome, " + name + "!");
}
void mainFunction() {
welcomeUser("Andy");
String name = "John";
welcomeUser(name);
}
Calling mainFunction() prints:
Welcome, Andy!
Welcome, John!
What happens to the arguments: Java passes by value
Java always passes a copy of the argument's value. For a primitive such as int, the method gets its own copy, so changing the parameter does not affect the caller's variable:
static void change(int x) { x = 99; }
int v = 1;
change(v);
System.out.println(v); // 1
For objects and arrays, the copied value is a reference to the same object. The method can therefore change the contents of an array it receives, while reassigning the parameter itself has no effect on the caller:
static void changeArr(int[] a) { a[0] = 99; }
static void reassignArr(int[] a) { a = new int[]{7}; }
int[] data = {1, 2};
changeArr(data);
System.out.println(data[0]); // 99
reassignArr(data);
System.out.println(data[0]); // still 99
More about parameters
- Variable number of arguments. A parameter written
int... numsaccepts any number ofintarguments and arrives in the method as anint[].sum(),sum(1)andsum(1, 2, 3)all work. It must be the last parameter. - Overloading. Several methods may share a name if their parameter lists differ.
greet(String s)andgreet(int n)are different methods, and the argument types pick which one runs. - Passing a function as a parameter. Java methods are not values by themselves. To pass behaviour, declare a parameter of a functional interface type, such as
Function<Integer, Integer>orRunnable, and call it with a lambda:apply(x -> x * 2, 21)prints42whenapplycallsf.apply(v). - Methods live in classes. In a full Java program, every method sits inside a class, and
mainis the entry point. The snippets above show only the methods themselves.
Common mistakes
- Forgetting the type in the parameter.
void welcomeUser(name)does not compile. Java needsString name. - Passing the wrong number or order of arguments, which produces the errors shown above.
- Expecting changes to a primitive to reach the caller. Assigning to the parameter changes only the local copy. To send a result back, return it.
- Missing the space in the text.
"Welcome," + nameprintsWelcome,Andy. - Writing the type in the call. The call is
welcomeUser(name), notwelcomeUser(String name). Types appear in definitions only.
Key takeaways
- Parameters are declared in the method definition. Arguments are the values passed in the call.
- Each parameter has a type, and arguments are matched by position, in the same order and count.
- Arguments can be literals, variables or expressions, and a variable in the call is separate from the parameter that receives its value.
- Java passes copies of values: a primitive is never changed by the method, and an object or array is shared through its reference.
