Java Argument unique argument
I have开发者_运维知识库 been working on Java off late, couldn't figure out what this (String... arg) means in
public static void main(String... arg) {}
...
is called varargs and is used to allow for variable number of function parameters.
For example if you declare a function as
void F(int a, int... arr) { }
Then you can call it as:
F(100, 4);
or
F(100, 4, 5);
or
F(100, 4, 5, 6);
The variable arr
is actually of type int[] in the body of the function and it contains the parameters, so arr = [4], arr = [4, 5] and arr = [4,5,6] respectively
It's varargs
They are known as Varargs (Variable arguments). This allows you to send a variable number of parameters. Varargs can be used for both ojects or primitives.
eg: void setArgs(int arg1, String... args)
Some things to notice are,
The vararg must be the last parameter of a method.
There can only be one vararg parameter in a method.
精彩评论