Several parameters
A method can have several parameters, separated by commas, each with its own type. The arguments in a call go to the parameters in order: the first to the first, the second to the second.
public class Main {
public static void main(String[] args) {
printSum(3, 4);
printSum(10, -2);
}
public static void printSum(int first, int second) {
System.out.println(first + " + " + second + " = " + (first + second));
}
}
3 + 4 = 7
10 + -2 = 8A call must give exactly as many arguments as there are parameters, and of fitting types. With one argument missing, it doesn't compile:
public class Main {
public static void main(String[] args) {
printSum(3);
}
public static void printSum(int first, int second) {
System.out.println(first + " + " + second + " = " + (first + second));
}
}
Main.java:3: error: method printSum in class Main cannot be applied to given types;
printSum(3);
^
required: int,int
found: int
reason: actual and formal argument lists differ in length
1 errorrequired is what the method's parameters need, and found is what the call gave. The parameters can have different types too: printPrice(String item, double price) is called as printPrice("Tea", 2.5).