• 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
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Confused with var-args.

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
arch rival
Posts: 2813
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Yogendra Joshi
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
The human mind is a dangerous plaything. This tiny ad is pretty safe:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic