About the definition of variable length parameters in Java and the use of the method detailed _java

Source: Internet
Author: User

In Java, variable-length parameters (Varargs) can be defined for a method to match multiple parameters of an indeterminate quantity, defined as "...". In fact, this is similar to passing an array for the method, and the same is the same as the arrays on the use method:

public void Test (string ... str) {for   
    (string s:str) {
      
    }
  }

The calling method is the same as normal invocation, except that the parameters that can be matched are 0 to many. As follows:

Test ();

Test ("Lilei");

Test ("Lilei", "Hanmeimei");

The use of the process to pay attention to several points:

1, when the call, if the same time can match the fixed parameters and variable length parameters of the method, will first match the fixed parameter method.

2. If you can match 2 simultaneous methods that contain variable parameters, the compilation will complain because the compiler does not know which method to call.

3, a method can have only one variable parameter, and the variable parameter should be the last parameter.

The above about the variable length in Java definition and use of the method is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.