(Java) What kind of argument is this? With a

56 views Asked by At

Please can anybody explain, I found this in a program on the Internet. I googled it up but nothing helped as I don't know what it is called?

Why are there three dots(...) after String in the argument? Please Explain.

public static void MakePro(String... visual) 
{
..
}
1

There are 1 answers

2
Ronak Jain On BEST ANSWER

Varargs (Variable Arguments) Varargs (introduced in Java SE 5) allows you to pass 0, 1, or more params to a method's vararg param. They let you pass any number of objects of a specific type.This reduces the need for overloading methods that do the similar things. For example,

public static void AsSimpleAsThis(String... params) 
// params represents a vararg. 
{
}

AsSimpleAsThis(s1,s2,s3); // pass 3 strings

params[0] is the first string

params[1] is the second string

params[2] is the third string

AsSimpleAsThis("hello",s2); // pass 2 strings

params[0] is the first string (="hello")

params[1] is the second string

AsSimpleAsThis("hey")

params[0] is the first string=hey

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. Varargs can be used only in the final argument position.