[Help] Meaning of "String..."

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

Thread Status:
Not open for further replies.
  1. Offline


    Just being curious, I saw some people using something like
    1. public void foo(String ...args) {}

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


    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. Offline


    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


    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


    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


    Here an example of the ... use (just wrote this for TrainCarts):
        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.
    double l = length(2, 3, 6, 3.6);
  7. Offline


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


    You can only put one in though and it has to be the last parameter
  9. Offline


    Thanks for that. :)

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

Share This Page