• 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

var arg

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'Its legal to have other parameters in a method that uses a var arg.'
does the above statement means


please explain.
 
Ranch Hand
Posts: 258
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Singh Himanshu wrote:'Its legal to have other parameters in a method that uses a var arg.'
does the above statement means


please explain.


http://lmgtfy.com/?q=java+varargs+tutorial

Varargs should be the last parameter
 
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Singh Himanshu wrote:'Its legal to have other parameters in a method that uses a var arg.'
does the above statement means


please explain.



the var-args will always be the last.

and also not both the args in the method are var-args.
 
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Singh Himanshu wrote:'Its legal to have other parameters in a method that uses a var arg.'
does the above statement means


please explain.




Yes, definitely more than one parameters can exist with one var-args in a method but one and only limitation is var-args must be the last parameter in the method declaration.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vishal Kashyap wrote:
Yes, definitely more than one parameters can exist with one var-args in a method but one and only limitation is var-args must be the last parameter in the method declaration.



No...we can only have one var-args argument in a method, and that should be the last.
more then one var-args will give compile time error as "too-many var-args"
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

shanky sohar wrote:. . . No...we can only have one var-args argument in a method, and that should be the last. . . .

That is the same as the previous poster said.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singh Himanshu, welcome to JavaRanch
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

shanky sohar wrote:. . . No...we can only have one var-args argument in a method, and that should be the last. . . .

That is the same as the previous poster said.



hmmm..Yes,you are right..last time i am not able to understand that.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and moreover, isnt int..x,int...y redundant?!
int...x itself means any number of int parameters.
infinity+infinity=infinity => so why not just use 'infinity' once
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And what about foo(String ... words, int ... numbers) . . .? That is also prohibited by the rule about one varargs parameter.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:and moreover, isnt int..x,int...y redundant? . . .

No it isn't; it is simply impossible to tell which the last int for x is and which the first int for y is.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:No it isn't; it is simply impossible to tell which the last int for x is and which the first int for y is.


Thats a reason why Java prevented more than 1 var-args for a method.
What I wanted to say was...
int... x => any number of integers
int... y => any number of integers
Even if more than 1 var-arg was allowed for a method, you wouldn't want to use int... x,int... y , because int... x simply means the same and would satisfy the requirement as well.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vinoth Kumar Kannan wrote:and moreover, isnt int..x,int...y redundant?!


No. For the method there is a real difference between x and y. So int... x, int... y is definitely not the same as int... xy.

Multiple varargs may sometimes be easy to detect (like String... s, int... i), but in too many cases (e.g. Object... o, String... s -- any String is also an object) there is just too much ambiguity. That's why only one is allowed.
 
Morning came much too soon and it brought along a friend named Margarita Hangover, and a tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic