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:
-
Array of objects is passed as an argument to method1(Animal…)
-
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.
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 asa[1]like you would any other array.If I have the following