The varargs in the Java language allows the caller to pass an indefinite number of arguments and pass in an indefinite number of arguments to the form parameter as an array.
So what is the value of the formal parameter without passing any arguments, or passing in null? Here is the test code and the result of the operation:
1 Private voidTest1 (int.. args) {2 if(Args! =NULL) {3System.out.println ("[test1] args.length =" +args.length);4}Else {5System.out.println ("[test1] args is null");6 }7 }8 9 Private voidtest2 (String ... args) {Ten if(Args! =NULL) { OneSystem.out.println ("[test2] args.length =" +args.length); A}Else { -System.out.println ("[test2] args is null"); - } the } - - Public void StaticMain (string[] args) { - test1 (); +Test1 (NULL); -Test1 (1); +Test1 (); A at test2 (); -Test2 (NULL); -Test2 ("1"); -Test2 ("A", "B"); -}
[Test1] args.length = 0
[test1] args is null
[Test1] Args.length = 1
[Test1] Args.length = 2
[Test2] args.length = 0
[test2] args is null
[Test2] Args.length = 1
[Test2] Args.length = 2
Conclusion:
- If the parameter is not passed, the value of the formal parameter is an array of length 0.
- If NULL is passed in, the value of the formal parameter is NULL, but there is a warning prompt at compile time.
VarArgs in the Java language