What is the ellipsis (...) for in this method signature?
Asked Answered
M

5

235

In the App Engine docs, what is the ellipsis (JID...) for in this method signature?

public MessageBuilder withRecipientJids(JID... recipientJids)

What's the function of those three dots?

Mainis answered 2/3, 2010 at 22:35 Comment(1)
By the way, I tried the HORIZONTAL ELLIPSIS character, , Unicode U+2026. NetBeans 8 in Java 8 reports "illegal character". So Java varargs require the triple FULL STOP (period), ....Marino
M
238

Those are Java varargs. They let you pass any number of objects of a specific type (in this case they are of type JID).

In your example, the following function calls would be valid:

MessageBuilder msgBuilder; //There should probably be a call to a constructor here ;)
MessageBuilder msgBuilder2;
msgBuilder.withRecipientJids(jid1, jid2);
msgBuilder2.withRecipientJids(jid1, jid2, jid78_a, someOtherJid);

See more here: http://java.sun.com/j2se/1.5.0/docs/guide/language/varargs.html

Muntin answered 2/3, 2010 at 22:38 Comment(3)
Note that the arguments may also be passed as an array of JID (this makes varargs backward compatible).Danyluk
Also note that passing 0 varargs (nothing) is legal.Crescendo
Last but not least, the vararg must be the last parameter of the method. So, you cannot have two vararg parameters in the same method. Maybe, you can find this method interesting docs.oracle.com/javase/7/docs/api/java/util/…Gone
E
82

The way to use the ellipsis or varargs inside the method is as if it were an array:

public void PrintWithEllipsis(String...setOfStrings) {
    for (String s : setOfStrings)
        System.out.println(s);
}

This method can be called as following:

obj.PrintWithEllipsis(); // prints nothing
obj.PrintWithEllipsis("first"); // prints "first"
obj.PrintWithEllipsis("first", "second"); // prints "first\nsecond"

Inside PrintWithEllipsis, the type of setOfStrings is an array of String. So you could save the compiler some work and pass an array:

String[] argsVar = {"first", "second"};
obj.PrintWithEllipsis(argsVar);

For varargs methods, a sequence parameter is treated as being an array of the same type. So if two signatures differ only in that one declares a sequence and the other an array, as in this example:

void process(String[] s){}
void process(String...s){}

then a compile-time error occurs.

Source: The Java Programming Language specification, where the technical term is variable arity parameter rather than the common term varargs.

Ernaldus answered 4/5, 2014 at 6:11 Comment(0)
L
28

The three dot (...) notation is actually borrowed from mathematics, and it means "...and so on".

As for its use in Java, it stands for varargs, meaning that any number of arguments can be added to the method call. The only limitations are that the varargs must be at the end of the method signature and there can only be one per method.

Lizettelizotte answered 2/3, 2010 at 23:1 Comment(0)
P
8

Those are varargs they are used to create a method that receive any number of arguments.

For instance PrintStream.printf method uses it, since you don't know how many would arguments you'll use.

They can only be used as final position of the arguments.

varargs was was added on Java 1.5

Pentatomic answered 2/3, 2010 at 22:48 Comment(0)
M
4

It means that the method accepts a variable number of arguments ("varargs") of type JID. Within the method, recipientJids is presented.

This is handy for cases where you've a method that can optionally handle more than one argument in a natural way, and allows you to write calls which can pass one, two or three parameters to the same method, without having the ugliness of creating an array on the fly.

It also enables idioms such as sprintf from C; see String.format(), for example.

Mulvaney answered 2/3, 2010 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.