Parameters & Arguments

The Course
•
4 min read

A parameter is a variable declared in a method’s parentheses. An argument is the value supplied by a call. In printSession("Java", 25), the string and integer are arguments. The method declaration names parameters for those values. Their types and order describe what callers must provide.

On each call, Java initializes new parameter variables from the argument values. Parameter names need not match names at the call site. If the declaration calls its integer minutes and main calls its integer duration, the call still works. Names communicate meaning within their own scope; types and positions connect the arguments to the parameters.

Trace one call at a time

A parameter lets one body work on different inputs without asking the caller to change data somewhere else in the program. Prefer explicit inputs so a reader can see what affects the output. Each invocation (one method call) starts with its own parameter values. For int values, changing a parameter inside the method does not assign a new value to the caller’s variable.

Arguments must match the expected types. Passing text “25” where an int is required is a type mismatch, even if it looks numeric. Likewise, swapping an int and a String changes which parameter receives each argument. Read the method name and declared parameter types—its signature—before choosing an argument order.

One operation, different inputs Java
public class Main {
    public static void main(String[] args) {
        printSession("Java", 25);
        printSession("Reading", 10);
    }

    static void printSession(String topic, int minutes) {
        System.out.println(topic + ": " + minutes + " min");
    }
}
Check your prediction
Java: 25 min
Reading: 10 min

The helper has no built-in knowledge of Java or reading. It describes whatever values the caller passes. Test a topic with spaces and a duration of zero; this helper currently formats data rather than validating it.

Changing an integer parameter is local Java
public class Main {
    public static void main(String[] args) {
        int minutes = 25;
        printIncreased(minutes);
        System.out.println(minutes);
    }

    static void printIncreased(int value) {
        value += 5;
        System.out.println(value);
    }
}
Check your prediction
30
25

value receives a copy of 25. The method changes its own local integer, not the variable in main. If the caller needs an updated value, the next lesson shows returning that value explicitly.

Compiler error: the arguments are reversed Java
public class Main {
    public static void main(String[] args) {
        printSession(25, "Java");
    }

    static void printSession(String topic, int minutes) {
        System.out.println(topic + ": " + minutes);
    }
}
Read the diagnostic

The first parameter expects a String and the second an int. Read the types in declaration order and repair the call. Renaming parameters would not fix the mismatch.

Your turn

Exercise

Repeat a supplied message

Write printRepeated(String message, int count). Use a for loop to print the message count times. Calling it with Java and 2 prints two Java lines; calling it with Done and 1 adds one Done line. A count of 0 prints nothing. Assume nonnegative counts for this task.

Your workspace Java
public class Main {
    public static void main(String[] args) {
        // Call printRepeated twice.
    }

    // Define a helper with a message and count parameter.
}
Hint 1: the idea

The loop decides how often, while the String parameter decides what to print.

Hint 2: a first step

Put count in the loop condition. Start the loop index at zero so count=0 naturally skips the body.

Show one solution
public class Main {
    public static void main(String[] args) {
        printRepeated("Java", 2);
        printRepeated("Done", 1);
    }

    static void printRepeated(String message, int count) {
        for (int index = 0; index < count; index++) {
            System.out.println(message);
        }
    }
}
Java
Java
Done

Change it and explain

Call the helper with 0, 1 and 3. Then put an int named count in main, pass it as an argument and print it after the call. Explain why the loop does not use up that caller variable.

A parameter is not input read from a keyboard: the caller supplies it. A later input-reading method can pass values to the same helper. Keeping those jobs separate will make both browser practice and automated checks easier.

Was this article helpful?

Your response is saved on this device.