Java Method Parameters
Parameters make methods flexible. Instead of a method that always greets "Ada", you write one that greets whoever you pass in. Parameters are declared in the parentheses after the method name, each with a type and a name.
Note: Strictly speaking, the variables in the declaration are parameters, and the actual values you pass in a call are arguments.
One parameter
class Main {
static void greet(String name) {
System.out.println("Good morning, " + name + "!");
}
public static void main(String[] args) {
greet("Ada");
greet("Linus");
String guest = "Margaret";
greet(guest);
}
}Good morning, Ada!
Good morning, Linus!
Good morning, Margaret!
Multiple parameters
Separate parameters with commas. Arguments must be passed in the same order, with matching types and the right count:
class Main {
static double totalPrice(String item, int quantity, double unitPrice) {
double total = quantity * unitPrice;
System.out.printf("%d x %s @ %.2f = %.2f%n", quantity, item, unitPrice, total);
return total;
}
public static void main(String[] args) {
double sum = 0;
sum += totalPrice("Notebook", 3, 2.50);
sum += totalPrice("Pen", 10, 0.80);
System.out.printf("Grand total: %.2f%n", sum);
}
}3 x Notebook @ 2.50 = 7.50
10 x Pen @ 0.80 = 8.00
Grand total: 15.50
Calling totalPrice(3, "Notebook", 2.50) would not compile — the types are in the wrong order.
Java passes arguments by value
When you call a method, each parameter receives a copy of the argument's value. For primitives this means the method can't change the caller's variable:
class Main {
static void tryToDouble(int x) {
x = x * 2;
System.out.println("inside: " + x);
}
public static void main(String[] args) {
int number = 21;
tryToDouble(number);
System.out.println("outside: " + number);
}
}inside: 42
outside: 21
For objects and arrays, the value that is copied is the reference. The method gets its own copy of the reference, but both point to the same object — so changes made through the reference are visible to the caller, while reassigning the parameter is not:
import java.util.Arrays;
class Main {
static void addBonus(int[] scores) {
for (int i = 0; i < scores.length; i++) {
scores[i] += 5; // modifies the shared array
}
}
static void replace(int[] scores) {
scores = new int[] {0, 0, 0}; // only the local copy changes
}
public static void main(String[] args) {
int[] myScores = {70, 85, 90};
addBonus(myScores);
System.out.println(Arrays.toString(myScores));
replace(myScores);
System.out.println(Arrays.toString(myScores));
}
}[75, 90, 95]
[75, 90, 95]
Variable arguments (varargs)
Writing type... name lets a method accept any number of arguments of that type. Inside the method, the parameter is an array:
class Main {
static int sum(int... numbers) {
int total = 0;
for (int n : numbers) total += n;
return total;
}
static void log(String level, String... messages) {
for (String m : messages) {
System.out.println("[" + level + "] " + m);
}
}
public static void main(String[] args) {
System.out.println(sum());
System.out.println(sum(4));
System.out.println(sum(1, 2, 3, 4, 5));
log("INFO", "Server started", "Listening on port 8080");
}
}0
4
15
[INFO] Server started
[INFO] Listening on port 8080
A varargs parameter must be the last parameter, and a method can have only one.
final parameters
Marking a parameter final prevents the method from reassigning it — a small safety net that some teams use everywhere:
static double withTax(final double net, final double rate) {
// net = 0; // error: final parameter net may not be assigned
return net * (1 + rate);
}Test yourself
In greet("Ada"), what is "Ada"?
- A parameter
- An argument
- A return value
- A field
After calling static void inc(int n) { n++; } with inc(x) where x = 5, what is x?
- 5
- 6
- 0
- Compile error
Which declaration is valid?
- static void f(int... a, String b)
- static void f(String b, int... a)
- static void f(int... a, int... b)
- static void f(...int a)