prudthvi
prudthvi

Reputation: 21

Varargs vs method overloading

Method overloading is a technique utilized to accomplish various tasks, each of which is conditioned by the number and types of variables passed as parameters. Each overloaded method contains different implementations based on the parameters passed. However, the need for varargs arises when we want to pass an arbitrary number of arguments. The question then arises: how can we distinguish between different tasks when an unspecified number of arguments are passed? How can we effectively handle various tasks under such a scenario?

Upvotes: 1

Views: 1237

Answers (4)

Arfur Narf
Arfur Narf

Reputation: 532

The clue is in your statement of the problem.

...which is conditioned by the number and types of variables passed as parameters.

A varargs function has an indeterminate number of objects of the same type, at least as far as the declaration of the function is concerned.

Overloading allows you to specify the actual types of the objects you want the function to apply to.

This it seems to me serves to make the distinction clear enough.

Further to that, if you have a small and fixed number of cases, if the choice has to be between overloading and varargs, then I'd lean to overloading. It provides better compile-time safety, in that any given call can be matched against the available functions, and an error given if there's no match.

But do you have to overload? In other words, is there some good reason for all these functions having the same name. If they all do substantially the same thing as far as the user is concerned, it's good. Otherwise, no - think up better names.

Don't use varargs if the acceptable calls have a small set of acceptable argument counts that you know in advance.

Lastly of course, it doesn't have to be overload or varargs; it can be both.

Upvotes: 0

anks
anks

Reputation: 1

Lets suppose you want to calculate volume of quadrilateral. First scenario is calculating area for rectangle, it will require three parameters. second scenario will be if you are calculating area for cube that requires only one parameter and third scenario could be no value is passed. Lets see through an example: For illustration purpose only

Class Volume(){

Volume(){
this (-1, -1, -1)
}
Volume(int x){
this (x, x, x)
}
volume(int x, y, z)
length = x;
breadth = y;
height = z;
}
public int getVolume(){
return length*breadth*height
}

This is known scenario and no other options are possible, but if you are unsure of number of arguments, we use varags. In the above situation we did not use varags as they are less efficient and consume more space and its more dangerous in the sense it lets user to pass as many arguments as they want which is not the case in the above example

Upvotes: 0

Keith
Keith

Reputation: 3151

VARARGS is used when there is an indeterminate number of parameters (objects) needed for a method. A canonical example is Java's Formatter.

An example is as such:

String.format("My name is %s", myName);
String.format("My name is %s %s", myFirstName, myLastName);
String.format("My name is %s %s and I am %d years old", myFirstName, myLastName, myAge);

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533720

varargs are a short hand for passing an array. e.g. instead of writing

public static void main(String[] args) {
   for(String arg : args)
       System.out.println(arg);

you can instead write

public static void main(String... args) {
   for(String arg : args)
       System.out.println(arg);

the main difference is that the caller can now write

main("hello", "world");

Upvotes: 2

Related Questions