Variable Length of Arguments in Java - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Random Posts

Variable Length of Arguments in Java

Share This

We have already discussed that Java supports method overloading. If you want to define many methods by the same name but with a different number of arguments or with different data types, you can do so. Now, try to understand that overloading is not an efficient way in solving many problems. Let us start with a case study.

Here, we want to define a class, which will be used to calculate the average of some integers. Based on the prior knowledge of method overloading, the class can be defined as follow.

Calculator.java
public class Calculator { public double average(int n1, int n2) { return ((double) (n1 + n2) / 2); } public double average(int n1, int n2, int n3) { return ((double) (n1 + n2 + n3) / 3); } public double average(int n1, int n2, int n3, int n4) { return ((double) (n1 + n2 + n3 + n4) / 4); } public double average(int n1, int n2, int n3, int n4, int n5) { return ((double) (n1 + n2 + n3 + n4 + n5) / 5); } public double average(int n1, int n2, int n3, int n4, int n5, int n6) { return ((double) (n1 + n2 + n3 + n4 + n5 + n6) / 6); } public double average(int n1, int n2, int n3, int n4, int n5, int n6, int n7) { return ((double) (n1 + n2 + n3 + n4 + n5 + n6 + n7) / 7); } public double average(int n1, int n2, int n3, int n4, int n5, int n6, int n7, int n8) { return ((double) (n1 + n2 + n3 + n4 + n5 + n6 + n7 + n8) / 8); } }
MyProg.java
public class MyProg { public static void main(String[] args) { Calculator cal = new Calculator(); System.out.println(cal.average(10, 15)); System.out.println(cal.average(10, 15, 22)); System.out.println(cal.average(10, 15, 22, 11)); System.out.println(cal.average(10, 15, 22, 11, 17)); System.out.println(cal.average(10, 15, 22, 11, 17, 18)); System.out.println(cal.average(10, 15, 22, 11, 17, 18, 20)); System.out.println(cal.average(10, 15, 22, 11, 17, 18, 20, 19)); } }

Look at the above code carefully. If we want to pass a different number of arguments in the average method, the class must have the respective method.

Then, how many overloaded methods will you define for calculating the average? It's not possible to define thousands of overloaded methods for one operation.

Here the solution is very simple. Define a single method that can receive any number of arguments and is able to perform the task.

Thus, the above class can be redefined in such a way that it will contain one method to perform the task instead of containing thousands of overloaded methods.

Hence, the program can be rewritten as follow:

Calculator.java
public class Calculator { public double average(int... nums) { int i = 0, sum = 0; for (int num : nums) { sum += num; i++; } return ((double) sum / i); } }
MyProg.java
public class MyProg { public static void main(String[] args) { Calculator cal = new Calculator(); System.out.println(cal.average(10, 15)); System.out.println(cal.average(10, 15, 22)); System.out.println(cal.average(10, 15, 22, 11)); System.out.println(cal.average(10, 15, 22, 11, 17)); System.out.println(cal.average(10, 15, 22, 11, 17, 18)); System.out.println(cal.average(10, 15, 22, 11, 17, 18, 20)); System.out.println(cal.average(10, 15, 22, 11, 17, 18, 20, 19)); } }

Note that a method may receive one or more arguments long with variable length of arguments. But the variable length of argument should be the last argument of the method and a method cannot accept more than one variable length of arguments.



Happy Exploring!

No comments:

Post a Comment