• 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

what's the use of var-args

 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone explain me where all can we use var-args.Cant we use array ?
I we cant then what's the difference between varargs and array decalration in method passing as parameters.
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,


The var-arg must be the last parameter in the method's
signature, and you can have only one var-arg in a method.



This is true factor. Where does your doubt lie? There can be only one
var-arg in a method parameter list and it must be last if there are more
that one parameter exist there.


Thanks,
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for reply but I want to know why we use var-args ,Cant we use array instead or might be I am not on track .
I am confused with the concept of var-args
Can you give me one example where we must use var-args instead of array.
Lot of confusion regarding that.
help me please
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,

Benefits of using var-arg:
1- Sometimes you don't need to pass arguments to the method that
requires var-arg. It is not mandatory to pass value to var-arg field.

2- var-arg is new arrival with Java 5.0, anyway it is converted to the
array finally by the compiler. It is just convenience to us.

Thanks,
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thanks for clearing my doubt but could you please elaborate on point below what you have written and explain me with example
Sometimes you don't need to pass arguments to the method that
requires var-arg. It is not mandatory to pass value to var-arg field.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Divya,

See the code below:



Note: It is called variable argument; number of arguments can vary
from 0 to any number.


Thanks,
[ July 05, 2007: Message edited by: Chandra Bhatt ]
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Do one more thing to see, var-arg is converted to array:

public class Test1 {
public static void main(String... args) {
System.out.println("Hello Test1");
}
}

> javac Test1.java
> javap Test1

You will see like:


Thanks,
[ July 05, 2007: Message edited by: Chandra Bhatt ]
 
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
Varargs are for convenience. Ofcourse you can use an array to do the same thing, but you would have to write more code and it looks more complex. Example:
 
Divya Gehlot
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank you very much for clearing my doubt.Now I got the usage of var-args.
 
Forget this weirdo. You guys wanna see something really neat? I just have to take off my shoe .... (hint: it's a tiny ad)
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic