Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: why java has introduced vararg in java 6 when we already have Array and collections?

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default why java has introduced vararg in java 6 when we already have Array and collections?

    Hi All,

    I was going through some new concepts introduced after java 4 and i am not sure why variable argument x... y... got introduced in java, was it really needed?? I mean when we already have many collection classes and Array then why to come up with something new. Also some what i did not like the syntax of it, i mean what dot dot dot ... couldn't they find out something better to represent it?

    Thanks


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: why java has introduced vararg in java 6 when we already have Array and collections?

    Think about it the other way: why not? It can be handy in many situations - the printf method of PrintStream is a good example: it would be quite a hassle to have to create an array or Collection every time I wanted to use this method. As for syntax, the ellipsis (...) is a common syntax in many other programming languages, so there's no reason to use something different.

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    78
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: why java has introduced vararg in java 6 when we already have Array and collections?

    Well, I agree with the above, why not? Which would you rather type?
    functionThatDoesThingsThatAreUsefulForThings(new int[]{ 1, 2, 69, 5})
    or
    functionThatDoesThingsThatAreUsefulForThings(1, 2, 69, 5)

  4. #4
    Junior Member
    Join Date
    May 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: why java has introduced vararg in java 6 when we already have Array and collections?

    Thanks for your reply. Above example looks ok if we have 4,5 or at the max 10 input params, but what if we have 100 params, isnt it a good idea to hold it in array or collection and iterate through...

  5. #5
    Junior Member
    Join Date
    Aug 2012
    Location
    Germany
    Posts
    19
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: why java has introduced vararg in java 6 when we already have Array and collections?

    If you ever see a coder adding 100 parameters to a method call - just kill him ;-)

    But yeah you're right it would be way better. Varargs is just syntactic sugar for fast calls like reflective invocations but they are not introduced to get rid of arrays everywhere (at least the compiler generates the array creation / element assignment for you right before the method call). Varargs are just array parameters and the method itself is flagged as being a vararg-method so that the last parameter is taken into account as a vararg one.

Similar Threads

  1. [SOLVED] Java array
    By maple1100 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 7th, 2013, 11:21 PM
  2. Java array calculation
    By maple1100 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 1st, 2013, 01:05 PM
  3. java array
    By fazmy3fvz in forum Java Theory & Questions
    Replies: 10
    Last Post: June 9th, 2012, 01:25 PM
  4. Replies: 4
    Last Post: November 14th, 2011, 10:00 PM
  5. Java Array
    By lary in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 12th, 2011, 09:29 AM

Tags for this Thread