Sub topic 2
Pass Arguments to Parameters
A method is a named set of instructions. You can call a method, or run it, whenever you need those instructions. Arguments let you give a method different values each time you call it.
A parameter is a named place for a value that a method receives. An argument is the value or expression you provide in a method call. The method uses that value through its parameter.
Write a method with parameters
The program below has a method that describes a visitor. It takes a name and a number of visits.
Save the code in a file named VisitDemo.java:
class VisitDemo {
static void describe(String name, int visits) {
System.out.println(name + " has " + visits + " visits.");
}
public static void main(String[] args) {
describe("Ava", 2);
String visitor = "Bo";
int count = 3;
describe(visitor, count + 1);
describe("Ava", 1);
}
}A class is a container for Java code. Here, the VisitDemo class contains two methods: describe and main. Java starts the program by running main. For now, leave its String[] args part as shown.
In the describe method:
staticmeans the method belongs to the class. This letsmaincall it directly.voidmeans the method does not give a result back to the caller.String namedeclares a parameter namedname. AStringis text.int visitsdeclares a parameter namedvisits. Anintis a whole number.
A type tells Java what kind of value a parameter can hold. Each parameter has its own type, and commas separate the parameters.
Trace the calls
To run the program, open a terminal in the folder containing the file. A terminal lets you enter commands. Run:
javac VisitDemo.java
java VisitDemoThe first command compiles the program, which means it prepares the Java code to run. The second command runs it. The output is:
Ava has 2 visits.
Bo has 4 visits.
Ava has 1 visits.Look at the first call:
describe("Ava", 2);
This call has two arguments. The first is "Ava", and the second is 2. Java gives them to the parameters in the same order:
| Call | Value for `name` | Value for `visits` |
|---|---|---|
| `describe("Ava", 2)` | `"Ava"` | `2` |
| `describe(visitor, count + 1)` | `"Bo"` | `4` |
| `describe("Ava", 1)` | `"Ava"` | `1` |
In the second call, visitor and count are variables, or named places that hold values. The argument count + 1 is an expression: code that produces a value. Since count is 3, the expression produces 4.
Java evaluates the arguments from left to right before it runs describe. Once describe finishes, the program continues after that call.
Match arguments to parameters
For each call, check the number, order, and types of the arguments:
describeneeds two arguments, because it has two parameters.- The first argument must be text that can be used as a
String. - The second argument must be a value that can be used as an
int.
For example, describe(2, "Ava") puts the arguments in the wrong order. Java will report a type error when you compile it. Order matters even when two parameters have the same type: a call may run but use the values in the wrong places.
The argument and parameter names do not need to match. In the second call, the argument visitor supplies the value for the parameter name.
Exercise
In describe("Mia", 5), which parts are arguments, and which parameters receive them?
Show answer
"Mia" and 5 are arguments. The parameter name receives "Mia", and visits receives 5.
Exercise
If count is 3, what does describe("Bo", count + 2) print?
Show answer
It prints Bo has 5 visits. because count + 2 produces 5.