aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confused with var-args. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confused with var-args." Watch "Confused with var-args." New topic
Author

Confused with var-args.

Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
May be this would be a ugly question , But its scratching my mind ! When we define var-ars , Is it an requierement to have three dots ? I mean like String a... , May be i am confused.

Would be great , if someone clears my doubt.


Meri Zindagi Hain Tab Tak.. Jab Tak Tera Sahara.... Har Taraf Tu Hi Tu Hain SAI Tera Hi Hain Nazara.....
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
Yeap.

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments. Varargs can be used only in the final argument position.
Marcus Green
arch rival
Rancher

Joined: Sep 14, 1999
Posts: 2813
This is the type of question you are probably best investigating by attempting to compile some code to see if it works. Here is an extract from my SCJP5 BOOK

VarArgs

When you see the term VarArgs you can think of it as �Variable number of method arguments�. It is another example of what some people dismissively call �syntactic sugar�, i.e. Something that sweetens the life of the programmer by passing some of the load onto the compiler and relieving the need to create standard �boiler plate� code.

The problem that VarArgs addresses is where you want to pass multiple values as parameters to a method. This problem is addressed in previous versions of Java by allowing you to populate an array and passing it as a parameter to a method. You can see this in action in the way the signature of the default main method accepts a String array, without requiring you to know how many parameters are going to be passed to the method. Passing an array is an acceptable solution to many situations, but for the programmer it represents a step that could be automated by the compiler/runtime and that is what the new VarArgs idea does.
The Syntax

The syntax for varargs is that you include three dots after the data type and before the name of the parameter. Those three dots are sometimes called �elipsis�. Note it is three dots and not two (as I mistakenly typed the first time I used this feature).


Note how this code uses the new version of the for loop to iterate through each element of the array that was created and passed to the method. You could treat the parameter in the same way as you would any other array, e.g. You could read its length property to find out the number of elements and walk through it using the original version of the for loop, but why work harder than strictly necessary?


SCWCD: Online Course, 50,000+ words and 200+ questions
http://www.examulator.com/moodle/course/view.php?id=5&topic=all
Yogendra Joshi
Ranch Hand

Joined: Apr 04, 2006
Posts: 213
Hi Wise Owen and Marcus Green ,

Its so nice to have an beautiful reply from you. Bert's Book definately had that ellipis but i was confused with three dots. Thanks for your immediate reply.

Regards ,

Yogendra Joshi.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with var-args.