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
Win a copy of
OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide
this week in the
Programmer Certification
forum!
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:
Tim Cooke
Campbell Ritchie
paul wheaton
Ron McLeod
Devaka Cooray
Sheriffs:
Jeanne Boyarsky
Liutauras Vilda
Paul Clapham
Saloon Keepers:
Tim Holloway
Carey Brown
Piet Souris
Bartenders:
Forum:
Beginning Java
var-args
suavedeep kaur
Ranch Hand
Posts: 36
posted 16 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 16 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 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
thanks
alot
nice example
Suavedeep kaur
SCJP
Campbell Ritchie
Marshal
Posts: 80673
478
posted 16 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 16 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 %
Did you see how Paul
cut 87% off of his electric heat bill with 82 watts of micro heaters
?
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...