Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java: varargs vs arrays

  • 19-09-2006 4:35pm
    #1
    Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭


    I am just wondering what is the point of the following.

    This...
    private static void Sample(int... i)
    

    vs
    private static void Sample(int[] i)
    

    as far as I can see the second one to me looks easier to read and with the varargs you have the limitation of that it has to be the last argument.

    Also it appears to cause problems with reading code for example..
    		printVars(10,12,15,22);
    		
    private static void printVars(int i1, int i2, int i3)
    private static void printVars(int... i)
    private static void printVars(long l, int... i)
    

    In this instance none of these methods will work, but even then it would be causing headaches. This makes more sense.
    private static void printVars(int i1, int i2, int i3)
    private static void printVars(int[] i)
    private static void printVars(long l, int[] i)
    
    The code would work in that case and it would be less ambiguous.

    I am just wondering of what exactly is the benefit of using varargs.


Comments

  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    In my opinion it makes the calling code slightly neater to read/understand.

    for example :
    private static int addnumbers1(int[] i) {
     ...
    }
    
    private static int addnumbers2(int... i) {
     ...
    }
     
    // Sample calling code
    private static void someCallingMethod() {
        int total1 = addnumbers1([b]new int[] { 1,40,5,8}[/b] ); // messy?    
        int total2 = addnumbers2(1,40,5,8); // cleaner!
    }
    
    
    


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Yea but how many times would you call a method that way? More often then not your already working with arrays.

    To make it more confusing if you removed the "addnumbers1(int[] i)" it will automatically revert to the other method (as you can pass in an array or comma delimited to it).

    In your sample code above "addnumbers2(int... i)" would never be called.


  • Registered Users, Registered Users 2 Posts: 4,188 ✭✭✭pH


    Hobbes wrote:
    Yea but how many times would you call a method that way? More often then not your already working with arrays.
    But when you're not it saves fairly ugly "new Object[] {a,b,c}" type code.

    The new printf is a good example when you'd be unlikely to be using arrays, or what about a generic(hypothetical!) sqlCall that can run one or more sql string sequentially?

    sqlUpdate(String... sql)
    To make it more confusing if you removed the "addnumbers1(int[] i)" it will automatically revert to the other method (as you can pass in an array or comma delimited to it).
    Agreed.
    In your sample code above "addnumbers2(int... i)" would never be called.
    Not sure about that!


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    pH wrote:
    But when you're not it saves fairly ugly "new Object[] {a,b,c}" type code.

    But only at method calling. You can just as easily do.

    int[] i = { 1, 2, 3 }
    method(i);

    About they only difference there is the curly braces.
    Not sure about that!

    Doh, yea your right. :)


Advertisement