Method Parameters and Arguments

Naveen AnamaneniUpdated · 4 min read

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

Courier.java▶ Try it
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);
    }
}
Output
Hyderabad -> Pune, 2.5 kg: Rs. 125.00
Pune -> Hyderabad, 1.0 kg: Rs. 80.00

How it works

  1. Arguments are matched to parameters by position: the first argument goes into from, the second into to, the third into weightKg.
  2. Java has no named arguments, so book(to: "Pune", ...) isn't possible. Order is everything.
  3. Line 10 passes the int 1 for a double parameter. 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:

Countdown.java▶ Try it
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);
    }
}
Output
3 2 1 Go!
start is still 3

The 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:

CallArgument
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

  1. Easy: Write static double bmi(double weightKg, double heightM) and print the BMI for 68 kg and 1.72 m, with two decimals.
  2. Harder: Write static String initials(String first, String last) that returns, for example, "N.A." for "Navika", "Anamaneni".

Quick check: Parameters and arguments

4 questions
1How are arguments matched to parameters in Java?
2Can you pass an int to a double parameter?
3A method decreases its parameter from. What happens to the caller's variable?
4In what order are arguments evaluated?

Key Takeaways

Was this tutorial helpful?

Lesson 83 of 482