Variable Arguments

Variable Arguments


  • Variable arguments have same functionality array. It can hold multiple similar objects in case of primitives. Similar object or subclass object in case of derived.

  • Example 1:

     
                    
                 class A
                 {
                    void test(int i)
                    {
                       System.out.println(“ test”);
                    }
                    void test(int I,int j)
                    {
                       System.out.println(“test with two args”);
                    }
                    void test(int I,int j,int k)
                    {
                       System.out.prinln(“3 args”);
                    }
                    public static void main(String args [])
                    {
                       A a=new A();
                       a.test(10);
                       a.test(2,24);
                       a.test(4,5,21);
                       a.test(32,4,24,5,5);
                    }
                 }
                   /*  It is one of the draw back inside method overloading with  */
                   /*  same type and with different arguments.  */
                   /*  This draw back can be overcomed  by using “varargs” concept.  */
                   /*  We can replace above as below  */
                 class A
                 {
                    void test(int … i)
                    {
                       int l=i.length;
                       if(l==1)
                       {
                          System.out.println(“1”);
                       }
                       else if(l==2)
                       {
                          System.out.println(“2”);
                       }
                       else
                       {
                          System.out.println(“int var args”);
                       }
                    }
                    public static void main(String args[])
                    {
                       A a=new A();
                       a.test();
                       a.test(19);
                       a.test2,4,5);
                       a.test(3,43,5,45,2,1);
                    }
                 }                

    Explanation:

    • If we have multiple calling statements for same method with same data type but different signature.

    • Same method, same data type with different list of arguments for each combination, we should require corresponding definition statement like class A with overloading test() with int type. But if you use variable argument we can satisfy all calling statement with single definition statement.

    • Note
       In method var args last parameter should be like below
       Void test

    Example 2:

     
                    
                 class  WrapMod
                 {
                    public static void main(String[] args) 
                    {
                       System.out.println("Hello World!");
                       Integer iw=new Integer(2);
                       Integer iw2=new Integer(2);
                       //System.out.println(iw+iw2);
                       System.out.println(iw.floatValue());
                    }   
                 }                

    Explanation:

    • In the above program, Integer iw=new Integer(2); is the calling statement for the Integer class with integer type constructor, in this we are passing int primitive value to it.

    • System.out.println(iw+iw2); is a compilation fail because we cant modify wrapper class instances(objects).

    • Note
       We can’t able to edit or modify the wrapper class instances(objects).