• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Not able to understand the rules of Var-args... please help...

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rule1)you must specify type of arguments that var-args parameter of your method can recieve
rule2)To declare a method using var-args parameter,you must follow the type with ellipsis(),space and then the name of the array that will hold the parameter recieved.
rule3)Its legal to have other parameters in a method that uses a var-args.
rule4)(This rule is confusing me the most). The var-arg must be last parameter in the methods signature,and you can have only one var-args in a method....
Can anyone explain me the 3rd and 4th rule...
void doStuff(char c,int...x) { } //this is legal
but then why below one is illegal..?
void doStuff(int ....y,char x) { }

void doStuff(String...s,byte b) { } //var-args must be last (I dint get)

Thanks...!
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rule4)(This rule is confusing me the most). The var-arg must be last parameter in the methods signature,and you can have only one var-args in a method....



If you have a method that takes var-arg as a parameter then var-arg must be the last parameter.
suppose you have a method that takes 2 parameter one byte and one var-arg then you can't write as



because here your method accepting var-arg as a first parameter. You should write it as




rule1)you must specify type of arguments that var-args parameter of your method can recieve



as in above code you must have to written that ypur var-arg param type is String
 
rizwana mujawar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another doubt about var-args...
Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)
 
rizwana mujawar
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much for those valuable replies:). Thanks once again:)
 
Pramod P Deore
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)



What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.

doStuff(String s) => Means method is accepting only one string parameter.
But doStuff(String.....s)=> Means method can accept any number of String parameter.



Thank you so much for those valuable replies. Thanks once again



You are Welcome
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod P Deore wrote:

Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)



What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.

doStuff(String s) => Means method is accepting only one string parameter.
But doStuff(String.....s)=> Means method can accept any number of String parameter.



And the varargs one can also accept a String[] parameter.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pramod P Deore wrote:

Should i always declare it void doStuff(String.....s). I mean those dots are compulsory...? or i can write it in void doStuff(String s)



What you get when you tried? That three dots are compulsarry. Because doStuff(String.....s) and doStuff(String s) both are different methods.



So, yes, you can write doStuff(String s), but then it's not a varargs method.
 
reply
    Bookmark Topic Watch Topic
  • New Topic