Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do three dots (...) indicate when used as a part of parameters during method definition? [duplicate]

What do three dots (...) indicate when used as a part of parameters during method definition?

Also, is there any programming term for the symbol of those 3 dots?

I noticed in a code sample:

public void method1 (Animal... animal) {
// Code
}

And this method was called from 2 places. The arguments passed while calling were different in both scenarios though:

  1. Array of objects is passed as an argument to method1(Animal...)

  2. Object of class Animal passed as an argument to method1(Animal...)

So, is it something like, if you are not sure whether you will be passing a single element of an array or the entire array as an argument to the method, you use 3 dots as a part of parameters in the method definition?

Also, please let me know if there is any programming term for the symbol of those 3 dots.

like image 463
Vikram Avatar asked Jul 27 '12 15:07

Vikram


People also ask

What is 3 dots mean in java?

The "Three Dots" in java is called the Variable Arguments or varargs. It allows the method to accept zero or multiple arguments. Varargs are very helpful if you don't know how many arguments you will have to pass in the method.

What does 3 dots mean in Typescript?

The three dots are known as the spread operator from Typescript (also from ES7). The spread operator return all elements of an array. Like you would write each element separately: let myArr = [1, 2, 3]; return [1, 2, 3]; //is the same as: return [...myArr];

What is the significance of the three dots in this function signature?

Rest operator. When used within the signature of a function, where the function's arguments should be, either replacing the arguments completely or alongside the function's arguments, the three dots are also called the rest operator.

What is triple dot in array?

When three dots (…) is at the end of function parameters, it's "rest parameters" and gathers the rest of the list of arguments into an array. When three dots (…) occurs in a function call or alike, it's called a "spread operator" and expands an array into a list.


3 Answers

It's called varargs.

It means you can pass as many of that type as you want.

It actually translates it into method1(Animal[] a) and you reference them as a[1] like you would any other array.

If I have the following

Cat whiskers = new Cat();
Dog rufus = new Dog();
Dolphin flipper = new Dolphin();

method1(whiskers, rufus, flipper); // okay!
method1(rufus); // okay!
method1(); // okay!
method1(flipper,new Parakeet()); // okay!
like image 60
corsiKa Avatar answered Oct 05 '22 16:10

corsiKa


That means that the method accepts an Array of that type of Objects but, that array is created automatically when you pass several Objects of that type separated by commas.

Keep in mind that there can only be one vararg parameter of a given type in a method signature, and you can't have another argument of the same type in the signature following the vararg (obviously, there would be no way of distinguishing between the two).

like image 26
pcalcao Avatar answered Oct 05 '22 18:10

pcalcao


It means that zero or more String objects (or an array of them) may be passed as the parameter(s) for that function.

Maybe:

x("foo", "bar");
x("foo", "bar", "baz");
like image 31
omar Avatar answered Oct 05 '22 17:10

omar