Pin It

How Variable Arguments List (Varargs) works in Java ?

Program to demonstrate how Variable Arguments List (Varargs) works in Java.

package varargs;

public class VarargsTest {

  public static void varargs( Object ... objects )
  {
     System.out.println("The Class name of objects is : " + 
                        objects.getClass().getName() );
  
     System.out.println("The length of objects is : " + 
                        objects.length  );
  
     for(int i = 0; i < objects.length; i++)
     {
 System.out.println("The value of objects : "+ objects[i]+" ");
     }
     
     System.out.println();
  }
 
  public static void main(String... args) {
  
     int day = 6;
     String month = "May";
     int year = 1986;
     args = new String[5];
  
     Object[] date = {day,month,year};
  
     varargs();
     varargs(day);
     varargs(day,month);
     varargs(day,month,year);
     varargs(date);
     varargs((Object) date);
     varargs(new Object[] {date});
     varargs(args);
     varargs((Object) args);
     varargs((new Object[]{args} ));
   
  }

}


Output of the program : 




 
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot