varargs was added in JDK 1.5 and has been used a lot of times in Java programs for allowing the user of API to decide the the arguments to be sent on the fly. Before we go and see the details of variable argument methods using code sample, let us see the salient points which should come handy while programming:

Important points

  • varargs have been implemented in many languages like C/C++ before Java.
  • Behind the scenes, varargs makes use of arrays.
  • This feature is upward compatible which means that clients calling methods with array can call methods with varargs too without any modification.
  • When using this feature, the clients can pass an array or a sequence of arguments
  • The syntax to use varargs is
  • varargs uses elipsis (…) which also known as variable arity parameter.
  • A vararg can be specified only as the last argument of a method
  • There is no specified length of arguments that can be passed to a vararg method and this length is limited by the number of elements that an array is allowed to hold by JVM.
  • You should not try overloading a vararg method because it can easily result in confusion for JDK as which argument is normal argument and which one is a part of vararg. (think of two method as test(String a, String… args) and test(String a, String b, String… args))
  • It is already being used in reflection and printf etc. in the JDK.

Sample Code

Now let us see a code sample for using variable arguments:

 
public class VarargsSample { 
    public static void main(String args[]) { 
        foo("a", "b", "c"); foo("b", "b", "c", "d"); 
    } 
    public static void foo(String a, String... args) { 
        System.out.print("\n" + a); 
        for(String arg: args) { 
            System.out.print(", " + arg); 
        } 
    }
}

De-compiled Code

The de-compiled version of the above program using cavaj De-compiler is:

 
public class VarargsSample { 
    public VarargsSample() { } 
    public static void main(String args[]) { 
        foo("a", new String[] { "b", "c" }); 
        foo("b", new String[] { "b", "c", "d" }); 
    } 
    public static transient void foo(String a, String args[]) { 
        System.out.print((new StringBuilder("\n")).append(a).toString()); String as[]; 
        int j = (as = args).length; 
        for(int i = 0; i < j; i++) { 
            String arg = as[i]; 
            System.out.print((new StringBuilder(", ")).append(arg).toString()); 
        } 
    } 
} 

As we can see in the de-compiled version that, the var args argument has been converted to an array.

Related Posts

Java Varargs or Variable Arguments admin Core Java
varargs was added in JDK 1.5 and has been used a lot of times in Java programs for allowing the user of API to decide the the arguments to be sent on the fly. Before we go and see the details of variable argument methods using code sample, let...
<p>varargs was added in JDK 1.5 and has been used a lot of times in Java programs for allowing the user of API to decide the the arguments to be sent on the fly. Before we go and see the details of variable argument methods using code sample, let us see the salient points which should come handy while programming:</p> <h2>Important points</h2> <p> <ul> <li>varargs have been implemented in many languages like C/C++ before Java.</li> <li>Behind the scenes, varargs makes use of arrays.</li> <li>This feature is upward compatible which means that clients calling methods with array can call methods with varargs too without any modification.</li> <li>When using this feature, the clients can pass an array or a sequence of arguments</li> <li>The syntax to use varargs is <type>...</li> <li>varargs uses elipsis (...) which also known as variable arity parameter.</li> <li>A vararg can be specified only as the last argument of a method</li> <li>There is no specified length of arguments that can be passed to a vararg method and this length is limited by the number of elements that an array is allowed to hold by JVM.</li> <li>You should not try overloading a vararg method because it can easily result in confusion for JDK as which argument is normal argument and which one is a part of vararg. (think of two method as test(String a, String... args) and test(String a, String b, String... args))</li> <li>It is already being used in reflection and printf etc. in the JDK.</li></ul> </p> <h2>Sample Code</h2> Now let us see a code sample for using variable arguments: 1 <h2>De-compiled Code</h2> The de-compiled version of the above program using cavaj De-compiler is: 1 As we can see in the de-compiled version that, the var args argument has been converted to an array.
Role of serialVersionUID in Serialization
Java Garbage Collection Tutorial
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.

Comments

comments