Summary: in this tutorial, you will learn about Java Varargs and how to use it to pass a variable number of arguments to a method.
Introduction to Java Varargs
Varargs stands for variable-length arguments. The varargs feature allows you to define a method that accepts a variable number of arguments of the same type.
Here’s the syntax for defining a method that has a variable number of parameters:
returnType methodName(type... args) {
// method body
}
Code language: Java (java)
In this syntax:
- First, specify the type for the parameter followed by three dots (…)
- Second, declare a parameter name.
Java allows a method to have zero or one varargs parameter. If a method has multiple parameters, the varargs must be the last parameter in the parameter list.
When calling the method, you can pass zero or more arguments to the varargs parameter:
methodName();
methodName(p1);
methodName(p1,p2,p3);
Code language: Java (java)
Internally, Java stores the arguments that you pass to the varargs parameter into an array args
. Therefore, you can access these arguments via the args
array in the body of the method.
Java Varargs examples
Let’s take some examples of using Java varargs to define methods with flexible parameters.
1) Basic Varargs example
The following example defines a method that calculates the sum of zero or more integers:
public static int sum(int...numbers) {
int total = 0;
for (int number: numbers) {
total += number;
}
return total;
}
Code language: Java (java)
In this example, the sum()
method can accept zero, one, two … integer arguments.
Inside the method, the numbers variable is an array of integers.
The method iterates over the elements of the numbers array and adds them up to the total variable.
When calling the sum()
method, you can pass any number of integers to it. For example:
public class App {
public static int sum(int...numbers) {
int total = 0;
for (int number: numbers) {
total += number;
}
return total;
}
public static void main(String[] args) {
var total = sum(1, 2, 3);
System.out.println("total=" + total);
}
}
Code language: Java (java)
Output:
total=6
Code language: Java (java)
In this example, we pass three integers 1, 2, and 3 to the sum()
method. It returns 6 as expected.
2) Using varargs with other parameters
If you use other parameters with varargs, the varargs parameter must be the last one in the parameter list.
For example, the following displayScores()
method shows a variable number of scores for a person:
public class App {
public static void displayScores(String name, int...scores){
System.out.println("Name: " + name);
for (var score: scores) {
System.out.print(score + " ");
}
System.out.println();
}
public static void main(String[] args) {
displayScores("John",5,4,5);
displayScores("Jane",5,3);
}
}
Code language: Java (java)
Output:
Name: John
5 4 5
Name: Jane
5 3
Code language: plaintext (plaintext)
In this example, the displayScores()
method has two parameters: name
and scores
. Since the scores
parameter is a varargs parameter, it appears at the end of the parameter list.
Summary
- Use Java varargs to define a method that includes a variable number of parameters of the same type.