[Help] Meaning of "String..."

Discussion in 'Plugin Development' started by Pandemoneus, Aug 20, 2011.

Thread Status:
Not open for further replies.
  1. Just being curious, I saw some people using something like
    Code:java
    1. public void foo(String ...args) {}


    I know that it somehow represents an array, but why the dots?
     
  2. Offline

    DrBowe

    If I'm not mistaken, the ...'s represent the fact that there is an unknown amount of parameters.

    It could be 3 strings, it could be 4, it could be 200.

    Of course, I could be wrong. So if anyone would like to correct me, be my guest :)
     
  3. So why not an array then instead of that? :p
    And how does Java handle "String...", couldn't find anything about it in google (it seems to ignore the dots, even when I order google to look for the exact expression) :p
     
  4. Offline

    Zeerix

    google "java variable argument list".

    You can call the method with as many arguments of the specified type as you want, and inside the method you can access the arguments as an array.
     
    Pandemoneus likes this.
  5. Offline

    DrBowe

    @Pandemoneus
    I've always thought that it's just easier to feed off strings as separate parameters, as opposed to constructing an array right then and there.

    In any case, arrays are not very flexible, so I'm sure that has something to do with this reasoning.
     
  6. Offline

    bergerkiller

    Here an example of the ... use (just wrote this for TrainCarts):
    Code:
        public static double length(double... values) {
            double rval = 0;
            for (double value : values) {
                rval += value * value;
            }
            return Math.sqrt(rval);
        }
    ... means that you can pass variables in it and the variables act as one array.
    Code:
    double l = length(2, 3, 6, 3.6);
     
  7. Offline

    escape

    Yep, that's how System.out.print() takes as many params as you throw at it.
     
  8. Offline

    Crash

    You can only put one in though and it has to be the last parameter
     
  9. Thanks for that. :)

    @Everyone else: Thanks for te examples aswell.
     
Thread Status:
Not open for further replies.

Share This Page