VarArgs in the Java language

Source: Internet
Author: User

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:

    1. If the parameter is not passed, the value of the formal parameter is an array of length 0.
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.