开发者

Java Argument unique argument

开发者 https://www.devze.com 2023-04-03 20:59 出处:网络
I have开发者_运维知识库 been working on Java off late, couldn\'t figure out what this (String... arg) means in

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号