Varargs in Java: Variable argument method in Java 5

java-logo-cupI 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.
Get our Articles via Email. Enter your email address.

You may also like...

18 Comments

  1. aravind says:

    Very Very Useful.

  2. surendra says:

    Very Good Explanation

  3. anisha says:

    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:

    HelloWorldVarargs.java:8: (identifier expected)
    public static void test(int some, String... args)
    
    HelloWorldVarargs.java:14: (identifier expected)
    }
    
    2 errors.
    

    please help me out…

    • deepthi says:

      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

  4. kanushi says:

    thanks for explaining in simple way ..

  5. samarth says:

    Good Job . Thank You!

  6. chandu says:

    Good Note..

  7. Babu says:

    Is this explaneion enough? I am expecting more abt varargs….can u update?

    Thanks in advance

  8. MrBCut says:

    Thank you sir. No, not too late, because it’s in the SCJP studyguide, so still good to know :)

  9. e0richt says:

    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?

  10. nami says:

    The article was good and easy-to-understand… But it would be more helpful if you also gave output of the program…

  11. nami says:

    for(String arg: args)
    Couldn’t understand this line… (I’m a newbie!)

    • Sanka Sumadura says:

      (string arg :args) 

      here arg is a string type variable,

      we can use any name to it
      like ,

      (string city: args) 

      ———————————————–
      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

      (string a:args) 


      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

    • Naeem says:

      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

  12. Lucky says:

    Thank you, very simple and effective explanation

  13. Prasad says:

    Hi,

    If i want to use multiple varargs such as

    public void getcust(String… A,Integer… i){}
    How can i achieve ?

  14. Vishal Jalgaonkar says:

    Simple and effective.

  15. simran says:

    wawww.. really its very helpfull..

Leave a Reply

Your email address will not be published. Required fields are marked *