Java variable length number of references varargs

Source: Internet
Author: User

Varargs (variable arguments) variable long parameters are features introduced in Java 1.5.

The form of the method is shown as print (String ... s), which is an arbitrary number of values.

public class varargsdemo{public    static void print (string ... s) {for        (string a:s)            System.out.print (a);    } Public    static void Main (string[] args) {        print ("a", "\ n");        Print ("A", "B", "\ n");        Print ("A", "B", "c\n");    }    }
Output:

A
Ab
Abc

Working principle:

1. Create an array with the length of the number of parameters passed by the caller;

2. Put the values of the arguments into the array,

3. Pass the Array (reference) to the modulated method.


Examples:

String.Format (String format, Object ... args)

The reflection mechanism.

"There are a lot of APIs in 7.3.2 that use variable-length parameters. I do not remember the introduction of programming, said the variable length of the number of references no, add the first "


Attention:

    • The variable length of the reference is to be the final form of the method;
    • Avoid overloading compilation errors.


Java variable length number of references varargs

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.