• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

var arg

 
Singh Himanshu
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Raymond Tong
Ranch Hand
Posts: 255
2
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • 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.
 
Vishal Kashyap
Ranch Hand
Posts: 73
BSD C++ Fedora
  • Mark post as helpful
  • send pies
  • 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
  • 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"
 
Campbell Ritchie
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Vinoth Kumar Kannan
Ranch Hand
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Pie
Posts: 48940
60
  • Mark post as helpful
  • send pies
  • 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
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic