Method Parameters and Arguments
A courier booking form has boxes: sender, receiver, weight. Whoever fills it in puts specific values in those boxes. A method works the same way. Its parameters are the labeled boxes, declared once; the arguments are the actual values filled in on each call. Getting them in the right order and of the right type is what makes the call work.
Matching by Position
public class Courier {
static void book(String from, String to, double weightKg) {
double charge = 50 + weightKg * 30;
System.out.printf("%s -> %s, %.1f kg: Rs. %.2f%n",
from, to, weightKg, charge);
}
public static void main(String[] args) {
book("Hyderabad", "Pune", 2.5);
book("Pune", "Hyderabad", 1);
}
}Hyderabad -> Pune, 2.5 kg: Rs. 125.00
Pune -> Hyderabad, 1.0 kg: Rs. 80.00How it works
- Arguments are matched to parameters by position: the first argument goes into
from, the second intoto, the third intoweightKg. - Java has no named arguments, so
book(to: "Pune", ...)isn't possible. Order is everything. - Line 10 passes the
int1 for adoubleparameter. Java widens it to 1.0 automatically (see "Type Casting and Type Conversion").
Try it yourself: swap the first two arguments on line 9. Does it compile? Is the result right?
Parameters Are Local Variables
Inside the method, each parameter is an ordinary local variable that starts with the argument's value. It exists only during that call, and changing it doesn't affect anything outside:
public class Countdown {
static void countdown(int from) {
while (from > 0) {
System.out.print(from + " ");
from--;
}
System.out.println("Go!");
}
public static void main(String[] args) {
int start = 3;
countdown(start);
System.out.println("start is still " + start);
}
}3 2 1 Go!
start is still 3The method counts its own from down to 0, but start in main stays 3, because from was a copy of its value. The lesson "Call by Value and Call by Reference in Java" explains exactly what is copied, including for arrays and objects.
Arguments Can Be Expressions
Anything that produces a value of the right type can be an argument: a literal, a variable, a calculation, or another method's result:
| Call | Argument |
|---|---|
book("Pune", "Goa", 3) | Literals |
book(city, home, weight) | Variables |
book(city, home, weight * 2) | An expression, worked out first |
book(city.trim(), home, readWeight()) | Results of other method calls |
Java evaluates the arguments from left to right, then calls the method.
Common Mistakes
Wrong order of arguments with the same types. book("Pune", "Hyderabad", 2.5) and the reverse both compile; only one is right. Clear parameter names help readers catch it.
Expecting a narrowing conversion. Passing a double to an int parameter doesn't compile; cast it, or change the parameter type.
Too many parameters. A method with seven parameters is hard to call correctly. Group related values into an object (Level 2).
Practice
- Easy: Write
static double bmi(double weightKg, double heightM)and print the BMI for 68 kg and 1.72 m, with two decimals. - Harder: Write
static String initials(String first, String last)that returns, for example,"N.A."for"Navika", "Anamaneni".
Key Takeaways
Was this tutorial helpful?