What is Varargs in Java?
![Java Course - Mastering the Fundamentals](https://www.scaler.com/topics/images/instructor-tarun.webp)
Varargs also known as variable arguments is a method in java that takes input as a variable number of arguments. It is used for simplifying a method that requires a variable number of arguments.
Importance of Java Varargs
- In Java, we can’t declare a method with a variable number of arguments until JDK 4. New methods were created in case of changes. Due to this, the length of the code was increased making it difficult to read. By the introduction of varargs these issues are solved.
- The variable-length arguments used to be handled in two ways before JDK 5, first is by using an overloaded method and the second is by putting the arguments in an array and then passing this array to a method.
- To solve all these problems, in JDK vararags were introduced which allowed declaring of methods with a variable number of arguments. It is by far the simplest and better option.
Syntax of Varargs
The varags method is implemented using a single dimension array internally. Hence, arguments can be differentiated using an index. A variable-length argument can be specified by using three-dot (...) or periods.
In this syntax, it tells the compiler that the method funMethod() can be called with zero or more arguments due to which, ‘a’ is declared as type int[] implicitly.
Examples
Working of Varargs
Implementing a method by using varargs in Java. In this, we will be creating a metod that takes multiple arguments in the method parameter, do the computation and returns the output.
Output:
Explanation:
- This syntax (...) tells the compiler that varargs have been used inside a method.
- The arguments are stored inside an array ‘a’ internally. On an array, variable ‘a’ is operated whose data type is int.
- This means that only int data type is accepted. To check the number of arguments or to find the length of an array, the a.length method can be used.
format() Method
Java format() method is widely used for formatting the string. There are many things that can be done using this method. For instance, concatenating the string and formatting the output of concatenated string.
Syntax:
This returns a formatted string of specified format and locale.
This returns a formatted string of specified locale and arguments.
Erroneous Varargs Example
1. Specifying two varargs in a single method
2. By specifying varargs as the first parameter instead of last
Overloading Varargs Methods
It is similar to that of overloading methods. Except in this, the varargs method is overloaded.
Example:
Output:
Things to Remember while using Varargs
- Always keep varargs at the end while defining the signature of the method else it will get executed every time before the default function.
- There should only be one varargs parameter in the method.
OR
Conclusion
- Varargs also known as variable arguments is a method in java that takes input as a variable number of arguments. It is used for simplifying a method that requires a variable number of arguments.
- In java, we can’t declare a method with a variable number of arguments until JDK 4. New methods were created in case of changes. Due to this, the length of the code was increased making it difficult to read. By the introduction of varargs these issues are solved.
- The variable-length arguments used to be handled in two ways before JDK 5, first is by using an overloaded method and the second is by putting the arguments in an array and then passing this array to a method.
- To solve all these problems, in JDK vararags were introduced which allowed declaring of methods with a variable number of arguments. It is by far the simplest and better option.
- The varags method is implemented using a single dimension array internally. Hence, arguments can be differentiated using an index. A variable-length argument can be specified by using three-dot (...) or periods.
- Things to remember while using Varargs:
- Always keep varargs at the end while defining the signature of the method.
- There should only be one varargs parameter in a method.