8.9 Methods Accepting a Variable Number of Arguments
Sometimes, the number of arguments to be sent to a method cannot be determined until run time. For example, we may have a method that validates inputs for other methods. The number of inputs may vary from one method call to the next.
In these cases, we can use a feature in the Java language named varargs. We specify in the method header that the method will accept a variable number of arguments by typing three dots immediately after the data type of the last parameter.
The syntax is the following:
accessModifier returnType methodName( (0 to many parameters,) dataType... lastParameter )
Note that there is no space between the data type and the three dots.
A parameter that a method specifies ...
Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.