Search This Blog

Saturday, May 19, 2012

Java varargs

New Feature of Java 5.0 is  "varargs" that enables methods to receive variable numbers of arguments. Variable-length argument lists are a new feature in J2SE 5.0. Programmers can create methods that receive an unspecified number of arguments. An argument type followed by an ellipsis (...) in a method's parameter list indicates that the method receives a variable number of arguments of that particular type.
Example :

public class VarargsDemo {

     * @author Piyush Chaudhari
    public void display(String...strings)
         System.out.println("Method Argument is :"+strings.length);
         for(String string:strings)
             System.out.println("Argument is :"+string);
    public static void main(String[] args) {
         // TODO Auto-generated method stub
         VarargsDemo demo=new VarargsDemo();        demo.display("Piyush","Chaudhari","Gujaratvidyapith","Ahmedabad");