I think I am late for writing about varargs. The feature of variable argument has been added in Java 5 since its launch. Still I will write something about varargs.
varargs has been implemented in many languages such as C, C++ etc. These functionality enables to write methods/functions which takes variable length of arguments. For example the popular printf() method in C. We can call printf() method with multiple arguments.
printf("%s", 50);
printf("%d %s %s", 250, "Hello", "World");
Code language: Arduino (arduino)
Varargs was added in Java 5 and the syntax includes three dots … (also called ellipses). Following is the syntax of vararg method.public void testVar(int count, String... vargs) { }
Code language: Java (java)
Notice the dots … in above code. That mark the last argument of the method as variable argument. Also the vararg must be the last argument in the method. Now let us check simple hello world varargs code.public class HelloWorldVarargs {
public static void main(String args[]) {
test(215, "India", "Delhi");
test(147, "United States", "New York", "California");
}
public static void test(int some, String... args) {
System.out.print("\n" + some);
for(String arg: args) {
System.out.print(", " + arg);
}
}
}
Code language: Java (java)
In above code, the test() method is taking variable arguments and is being called from main method with number of arguments. So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.
Very Very Useful.
Very Good Explanation
Sir i dont knw why but its giving two errors. the same happens with any vargs program. i”m new to java programming so please help me out..
the two errors are:
please help me out…
check the version of jdk if it is 1.6 or 7 you domt face problem since it( varags was introduced in 1.5 the implementation might be provided in 1.5 or 6 ,7
thanks for explaining in simple way ..
Good Job . Thank You!
Good Note..
Is this explaneion enough? I am expecting more abt varargs….can u update?
Thanks in advance
Thank you sir. No, not too late, because it’s in the SCJP studyguide, so still good to know :)
I have a question… the example you show looks like it boils down to an array of string….
I know that variable arguments in c++ allow for mixed types:
printf(“%s %d %f”, string1, int1, float1);
I would assume that java also allows for this? and if so then how does one pull them out of the list?
The article was good and easy-to-understand… But it would be more helpful if you also gave output of the program…
for(String arg: args)
Couldn’t understand this line… (I’m a newbie!)
here arg is a string type variable,
we can use any name to it
like ,
———————————————–
in those codes ” args” is the array name what we prepared in the method “test”
the “args” array contains the arguments when we call the method “test”
e.x= at the first calling of method- args array contains “India” and “Delhi”
at the code
the string type variable a assigns the elements of args array,
at first variable a=”India”;
then print it
after that
variable a=”Delhi”;
then print it.
I think now u have a small idea about vargs
That is For each loop in java
like
for(int temp:a)
System.out.println(temp);
where a is array contain list of integer number
Thank you, very simple and effective explanation
Hi,
If i want to use multiple varargs such as
public void getcust(String… A,Integer… i){}
How can i achieve ?
Simple and effective.
wawww.. really its very helpfull..