6

I am taking a Data Structures and Algorithms course for fun at a local community college. The course's textbook is Y. Daniel Liang's Introduction to Java Programming, 10th Edition. The book, itself, is pretty solid.

In dealing with Java.util.Arrays, Liang mentions Java's "variable-length" parameter. He writes (p. 265):

Java treats a variable-length parameter as an array. You can pass an array or a variable number of arguments to a variable-length parameter. When invoking a method with a variable number of arguments, Java creates an array and passes the arguments to it.

An example being:

public static void (int... toes) {
    //... some code
}

However, Liang never explains the origin or advantage of the variable-length parameter. If, as Liang says, the variable-length parameter is "converted" to an array, what is their advantage? Is there a software design pattern or engineering goal that is facilitated by the variable length parameter?

In other words, what does the above code snippet offer that is not offered by the below:

public static void (int[] toes) { // ...
4
  • 3
    One benefit is that using '...' gives you the option to not pass in any parameters at all. Commented Aug 30, 2015 at 19:05
  • @Zar I did not know that. Is that somewhat akin then to optional parameters in VB, etc? Commented Aug 30, 2015 at 19:06
  • I haven't used VB, but in a sense it is "optional." Also, another thing is that it lets you pass in parameters without having to convert them into an array first. Also note that '...' parameters must be the last parameter in your method, while array parameters can be anywhere. Commented Aug 30, 2015 at 19:08
  • 1
    I wouldn't have thought it is like an optional parameter, as if you don't pass a value, the parameter is considered null. Having var-args, allows for some nice API design too, say you want a method to process ints, and it most have at least one int, you could create the method as method(int initial, Integer... others); When it is called it might be called method(1), method(1, 2) or method(1, 2, 3), they would all be valid Commented Aug 30, 2015 at 19:18

2 Answers 2

10

The '...' parameters you are talking about are called varargs.

Some differences to note:

  • varargs can be passed no parameters (basically ignored), null, or an indeterminate number of arguments, while array parameters must be passed an array or null
  • varargs must be the last parameter of your method, whereas it doesn't matter for array parameters. This is because of this special property of varargs, which is probably the most significant difference between the two things you posted:

The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments

Source

So for example, this method:

public void myMethod(String... args) {}

Could be called with either of these:

String[] myStrings = {"a", "b", "c"};
myMethod(myStrings);
myMethod("a", "b", "c");

See this for a good explanation on when varargs should be used.

Sign up to request clarification or add additional context in comments.

Comments

5

In Java, varargs are a syntactical sugar for creating an array when calling a method. For example, these two calls are equivalent:

void foo(String... args) { ... }

foo("hello", null, "world", xyz);  // Java 1.5+
foo(new String[]{"hello", null, "world", xyz});  // All versions of Java

Varargs doesn't make anything new possible (by definition of syntactic sugar), but it reduces verboseness and makes some constructs much more agreeable to implement. For example, some of my favorite uses of vararg include: PrintStream.printf(), String.format(), Method.invoke().

Other good applications of varargs:

// This one is in the Java standard library
Collections: void addAll(Collection<? super T> c, T... elements);

// Custom examples
int max(int... nums);
void doOperation(File x, String y, SomeEnum options...);

Additionally, Java's varargs bring the language up to parity with the vararg support in C, Python, JavaScript, and other languages. So if a frequently recurring design (such as max()) works best with varargs, Java is no longer the odd language that requires an uglier implementation.

1 Comment

I deleted my previous comment (just so you are aware) and accepted your answer. While another answer with higher votes certainly provides great detail, your answer deals more specifically with the OP question: ...what is their advantage? Is there a software design pattern or engineering goal that is facilitated by the variable length parameter?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.