Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
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
Forum:
Beginning Java
var-args
suavedeep kaur
Ranch Hand
Posts: 36
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hi please can anyone explain me the functioning of var-args with an example ???
Suavedeep kaur
SCJP
Swastik Dey
Bartender
Posts: 2270
20
I like...
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
var args can be used to call a method with different number of arguments
class A{ public static void main(String args[]){ //called with 3 arguments printStrings("1","2","3"); //called with 1 argument printStrings("XYZ"); //called with 2 arguments printStrings("a","b"); } public static void printStrings(String... arg){ for(String x:arg){ System.out.println(x); } } }
Swastik
suavedeep kaur
Ranch Hand
Posts: 36
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks
alot
nice example
Suavedeep kaur
SCJP
Campbell Ritchie
Marshal
Posts: 79151
377
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Nice example, but you ought to add two warnings.
You can get no end of confusion if you also have a method like printStrings(
String
s2, String s2, String s3)
You cannot use varargs where there might be confusion with a method like printStrings(String[] arg)
gurneeraj singh
Ranch Hand
Posts: 80
posted 15 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If you are really confused that which method will be called, always remember that var-args method is the last option used by
java
.
example:
class A{ void stuff(int a, int b, int c){ System.out.println("method A") } void stuff(int...a){ System.out.println("method B") } } class B{ public static void main(String...args){ A ob=new A(); ob.stuff(1,2,3); } }
In above example "method A" will be printed.
There are some rules related to Var-args do read them aswell.
SCJP 5.0 93%<br /> <br />SCWCD 5.0 97 %
With a little knowledge, a
cast iron skillet
is non-stick and lasts a lifetime.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Vararg Problem
Question about Boxing
var-args overloading compiler error: ambiguous
Constructor Basics Doubt. K &B Pg131
Varargs overloading problem
More...