Java variable length parameter

Source: Internet
Author: User

Variable-length parameters are provided starting from JAVA5, and variable-length parameters can be used as data

    • Definition of variable length parameter method

Use... Represents variable-length parameters, such as

Print (String ... args) {

...

}

In a method with variable length parameters, parameters can be used as arrays, for example, all parameter values can be looped out.

Print (String ... args) {

for (String Temp:args)

SYSTEM.OUT.PRINTLN (temp);

}

    • Invocation of a variable-length parameter method

Any number of arguments can be given at the time of invocation, for example:

Print ("Hello");

Print ("Hello", "John Doe");

Print ("Hello", "Zhang San");

    • Precautions
    1. When invoking a method, if it can match the method of the fixed parameter and can match the method of variable length parameter, then Selecting fixed Parameters the method.
    2. A method can only be made up of a variable with an argument that is variable length and must be the last parameter.
    3. If the method to be called can match two mutable parameters, an error occurs when running, and the compilation can be

Java variable length parameter

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.