• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Varargs

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can any one explain why varargs is considers as array ?

Ex: O/P- varargs

public class MyVarargs {

public static void main(String... args) {

System.out.println("varargs");
}

}
 
srinivas.b
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Addition to the above mail
what is the main purpose of varargs?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can any one explain why varargs is considers as array ?

There's no special logical reason, that's just how the designers of the Java language decided to implement it.

what is the main purpose of varargs?

Sometimes you want a method with a variable number of arguments in a program. One example of this is the method String.format() (look it up in the API documentation). Before varargs, if you wanted to pass a variable number of arguments to a method, you could do that with an array:

But that syntax is quite cumbersome - you'll have to create a new Object[] each time. With varargs, you can write code that is shorter and more clear.
reply
    Bookmark Topic Watch Topic
  • New Topic