Methods become much more useful when you can pass data into them. Parameters are typed inputs declared inside the parentheses of a method declaration. Each parameter has a type and a name, and the method can use the parameter just like a local variable.
public static String makeLabel(String name, Integer count) {
return name + ' (' + count + ')';
}
This method declares two parameters: a String named name and an Integer named count. Multiple parameters are separated by commas, and each one needs its own type.
The two words are often mixed up, but they mean different things:
String name'Widget'When you call a method, each argument is copied into the matching parameter in order:
String label = makeLabel('Widget', 3);
// Inside the method, name is 'Widget' and count is 3
// label is now 'Widget (3)'
The first argument 'Widget' lands in the first parameter name, and the second argument 3 lands in the second parameter count. The types must match: passing an Integer where a String is expected causes a compile error.
Parameters let one method handle many different inputs. Instead of writing one method per customer name, you write a single method that greets any name you pass in.