Java Variable Arguments-tutorial
In Java, methods usually take a fixed number of parameters. But in real-world programming, we may not always know how many arguments we need to pass.
Example
- Sometimes you want to calculate the sum of 2 numbers.
- Other times, the sum of 4 numbers.
- Or maybe even 10 numbers.
Writing separate methods for each case (method overloading) is repetitive and hard to maintain.
To solve this, Java introduced Varargs (Variable Arguments) in Java 5. With varargs, a method can accept zero, one, or many arguments of the same type, making your code more flexible, shorter, and readable.
What are Varargs?
You can pass any number of arguments (including none).
Only one varargs parameter is allowed in a method.
The varargs parameter must be the last parameter.
Example
Output
Here, the same method sum() works for different numbers of arguments.
Example
Output
Advantages of Varargs
Makes code cleaner and more readable.
Allows methods to be flexible.
Helpful when the exact number of arguments is not known in advance.
What are Varargs?
Varargs (short for Variable Arguments) in Java allow a method to accept a variable number of arguments of the same type without the need to manually create an array. Behind the scenes, varargs are implemented as arrays, but they provide a much cleaner and more concise syntax when calling methods.
You can think of varargs as a flexible container that automatically adjusts its size based on how many values you pass to the method.
Syntax
Syntax
returnType methodName(dataType... variableName) { // method body}
... (three dots) defines a varargs parameter.
You can pass any number of arguments (including none).
Only one varargs parameter is allowed in a method.
The varargs parameter must be the last parameter.
Example
class VarargsDemo { static int sum(int... numbers) { int result = 0; for (int n : numbers) { result += n; } return result; }
public static void main(String[ ] args) { System.out.println("Sum of 2 numbers: " + sum(5, 10)); System.out.println("Sum of 4 numbers: " + sum(1, 2, 3, 4)); System.out.println("Sum of no numbers: " + sum()); }}
Output
Sum of 2 numbers: 15
Sum of 4 numbers: 10
Sum of no numbers: 0
Here, the same method sum() works for different numbers of arguments.
Example
class Greeting { static void greet(String message, String... names) { for (String name : names) { System.out.println(message + ", " + name + "!"); } }
public static void main(String[ ] args) { greet("Hello", "Alice", "Bob", "Charlie"); greet("Welcome"); // works even without names }}
Output
Hello, Alice!
Hello, Bob!
Hello, Charlie!
Welcome
Notice that the method works with multiple names and even when no names are passed.
Key Points
- A method can have only one varags parameter.
- The varags parameter must be the last parameter in the method.
- Varags can accept zero, one or multiple arguments.
- Internally, the compiler converts varags into an array.
- Use varags when the number of inputs is not fixed.
Advantages of Varargs
Reduces the need for method overloading.
Makes code cleaner and more readable.
Allows methods to be flexible.
Helpful when the exact number of arguments is not known in advance.
Limitations of Varargs
Slight performance overhead (because an array is created internally).
Overuse can make the code less clear.
Must always be used as the last parameter in a method.
Overuse can make the code less clear.
Must always be used as the last parameter in a method.
Real-World Use Cases
Logging frameworks - log(String... messages)
Mathematical operations - max(int... values)
Utility functions - Printing any number of strings or numbers
Two-Minute Drill
- Varargs allow a method to accept variable number of arguments.
- Syntax - returnType methodName(type. . . varName)
- Can accept 0, 1 or many arguments.
- Internally, they are stored as an array.
- Only one varargs parameter allowed, and it must be the last parameter.
- Useful in cases where the number of inputs is not fixed.