What do 3 dots next to a parameter type mean in Java?
In Java, the three dots (...
) next to a parameter type indicate that the parameter is a varargs parameter. A varargs parameter is a parameter that can accept zero or more values of a specified type.
Here's an example of how you can use a varargs parameter in a method:
public void printNumbers(int... numbers) {
for (int number : numbers) {
System.out.println(number);
}
}
This code defines a printNumbers
method that takes a varargs parameter of type int
. The method iterates over the numbers in the varargs parameter and prints them to the console.
To call this method, you can pass one or more integers as arguments:
printNumbers(1, 2, 3); // prints 1, 2, 3
printNumbers(4, 5, 6, 7); // prints 4, 5, 6, 7
printNumbers(); // does not print anything
The varargs parameter allows the printNumbers
method to accept any number of integer arguments.
I hope this helps! Let me know if you have any questions.