Parameters
A method can take parameters: values it gets from the call. They're declared in the parentheses, each with a type and a name, and inside the method they work like variables:
public class Main {
public static void main(String[] args) {
greet("Ada");
greet("Linus");
String name = "Grace";
greet(name);
}
public static void greet(String name) {
System.out.println("Hello, " + name + "!");
}
}
Hello, Ada!
Hello, Linus!
Hello, Grace!String nameingreet(String name)is the parameter. Each call gives it a value:greet("Ada")runs the method withnameholding"Ada".- The value in the call is the argument. It can be a literal, a variable or a calculation, such as
greet(first + " " + last). - The argument must fit the parameter's type:
greet(42)doesn't compile, because 42 isn't a String.
The same method now does something different on each call, depending on what it's given.
Challenges that call your method
From here on, some challenges ask for a method, and the check calls it directly. The task card shows the call it makes, such as countdown(3);, and what that call must print. Your own main can call the method however you like: that's what Run with my input runs.