Before you learn about Var-args it is better to understand the different about parameters and arguments. Following example demonstrate a use of these things.
Now you can see, what the arguments are and what the parameters are.
- Arguments are the things you specify between the parentheses you are invoking a method.
- Parameters are the things the method's signature that in indicate what the method must receive when it's invoked.
Var-args standards for Variable Arguments. It means in this section we are going to talk about special arguments called variable arguments (Var-args).This is introduced in Java version 5.0.This concept allows you to create methods that can take a variable number of arguments.
Rules for var-args
- You must mention the type of the argument of the var-args parameter that your method can receive.
- You must type the var-arg method as I mentioned above (var-args parameter…
name of the array).
- Void getName ( int… x );
- Void doThing ( Vehicle… vhl );
- There can be other parameters also uses a var-args.
- The var-args must be the last parameter in the method’s signature.
- You can have only one var-args in a method.
There are examples for legal and illegal declarations.
void getName(int… x);
void getName(float f,int… x);
void getName(char c,double d,int.. x);
void getName(Vehicle… vhl);
void getName(int x…); //syntax error
void getName(int… x,float f); //var-args must be placed in last
void getName(int… x,char… c); //only one var-arg is allowed
Try following example to understand about var-args.