Demystifying Variable-Length Arguments with Simple Examples
Demystifying Variable-Length Arguments ( Varargs ) with Simple Examples
👋 Hello, fellow Java enthusiasts! Welcome to our third article at Java Interview Pro! Today, we'll explore the fascinating world of Variable-Length Arguments in Java, commonly known as Varargs. Whether you're new to this concept or need a refresher, we'll break it down with plenty of examples to help you understand and leverage Varargs effectively in your Java code. Let's dive in!
Introduction to Varargs:
Variable-Length Arguments (Varargs) were introduced in Java with the release of Java 5 (J2SE 5.0) in September 2004.
In Java, Variable-Length Arguments (Varargs) allow methods to accept a variable number of arguments of the same type. Varargs are denoted by an ellipsis (...) after the argument's data type. This feature provides flexibility when you don't know how many arguments a method may receive. Let's get started with the basics:
The syntax of a method using Varargs is as follows:
ReturnType methodName(DataType... parameterName) {
// Method body
}Let's create a method to calculate the sum of integers using Varargs. In this example, we can pass any number of integers to the method, and it will compute their sum.
public class VarargsExample {
public static int sum(int... numbers) {
int total = 0;
for (int num : numbers) {
total += num;
}
return total;
}
public static void main(String[] args) {
int result = sum(1, 2, 3, 4, 5);
System.out.println("Sum: " + result); // Output: Sum: 15
}
}Varargs can coexist with other parameters in a method. However, Varargs should be the last parameter, as shown in the following example:
public class VarargsExample {
public static void printValues(String message, int... values) {
System.out.print(message + ": ");
for (int val : values) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
printValues("Even Numbers", 2, 4, 6, 8);
printValues("Prime Numbers", 2, 3, 5, 7, 11);
}
}Varargs can also handle an empty argument list gracefully. In such cases, the array passed to the method will be of length zero.
public class VarargsExample {
public static void printValues(int... values) {
if (values.length == 0) {
System.out.println("No values to print.");
return;
}
System.out.print("Values: ");
for (int val : values) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
printValues(); // Output: No values to print.
printValues(1, 2, 3); // Output: Values: 1 2 3
}
}Variable-Length Arguments (Varargs) is a powerful feature in Java that allows methods to handle a variable number of arguments of the same type. With Varargs, you can write flexible and convenient methods that adapt to different argument lengths.
In conclusion, the following is a valid script too, Instead of String[] args, we used variable length arguments
package org.example;
public class Main {
public static void main(String... args) {
System.out.println("Hello world!");
}
}We hope this article has demystified Varargs and provided you with a solid understanding of its capabilities. Don't forget to subscribe to Java Interview Pro for more Java insights, tips, and code examples. Happy coding! 💻

