Last Updated: 27 October, 2023
Varargs, which means variable argument, is a feature that was introduced in the Java 5 version that allows methods to accept a variable number of arguments.
To define a varargs method, simply add the ellipsis (...) operator to the end of the parameter type for the varargs parameter.
Varargs allows a method to accept none or multiple arguments.
Syntax of Varargs:
Before varargs, we were using two different approaches to achieve this functionality:
When we define a method with varargs (...), the Java compiler creates an array with generic type components to hold the arguments.
Only one variable argument (varargs) is allowed in one method.
The varargs argument must be the last argument in a method.
Only put … (3 dots). If the dots are less or more than 3, then it gives a syntax error.
Here are some examples of using varargs in Java:
Example 1: A Java program to demonstrate varargs
Output
Total Added Values: 0
Total Added Values: 10
Total Added Values: 30
Total Added Values: 60
Total Added Values: 100
Example 2: Ambiguity in Varargs Method Overloading
Vararg methods can also be overloaded in Java, but overloading may lead to ambiguity. Let's understand it using the below example.
Output
Exception in thread "main" java.lang.Error: Unresolved compilation problems: The method addNumbers(int[]) is ambiguous for the type VarArgsExample2 The method addNumbers(int[]) is ambiguous for the type VarArgsExample2 at com.varargs.example.VarArgsExample2.main(VarArgsExample2.java:23)
In this example, the compiler gets confused if we call the addNumbers(10, 20) method because addNumbers() is an overloaded method. The compiler doesn’t know which method to call. The compiler may think we are calling addNumbers(int... number) with one varargs argument. In the same way, the compiler may think we are calling addNumbers(int n, int... number) with the argument passed to the first argument with an empty second argument.
Since there are two possibilities, it causes ambiguity. To avoid ambiguity in the program, we need to use two different method names instead of overloading the varargs method.
Varargs can facilitate the creation of flexible and reusable code. Nevertheless, it is essential to use varargs with caution to avoid common pitfalls such as:
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com
Which of the following are valid declarations of varargs?
Ans.
Method Syntax | Result |
---|---|
void method(int... num){}; | Valid and standard declaration |
void method(int ... num){}; | Valid but not standard declaration |
void method(int ...num){}; | Valid but not standard declaration |
void method(int.. num){}; | Invalid |
void method(int.. . num){}; | Invalid |
void method(int.... num){}; | Invalid |
void method(int... num, int num2){}; | Invalid |
void method(int... num1 int... num2){}; | Invalid |
Can we overload the method as follows?
void method(int... marks){};
void method(int[] marks){};
Because Java internally treats varargs as arrays, both method declarations will generate the same byte code, which would result in ambiguity while determining call binding.
What is the use of var args in Java programming?
Varargs are used when we are not sure about the number of arguments a method may need. Internally, Java treats them as arrays.