Variable length parameter

Source: Internet
Author: User

The following requirements are first: return the largest of the arguments. However, because the number of arguments is uncertain, it is difficult to use overloaded methods.

Workaround: When defining a method, the format of the formal parameter is defined as follows:

int getmax (int first,int ... VarArgs) {

for (int i:varagus)

....

}

Call:

System.out.println ("max (1) =" +demo.getmax (1) + "");//1
System.out.println ("max (2) =" +demo.getmax (2,1) + "");//2
System.out.println ("max (3) =" +demo.getmax (6,4,5,9,8,7) + "");//9

Description

1, variable-length parameters are actually compiled to convert the array.

2, the variable length parameter can only appear in the formal parameter.

3, if in the overloaded method, some have variable-length parameters, some do not, then priority to call the method without variable length parameters. If you define an int getmax (int first), then this method takes precedence

4, if the overloaded method contains variable length parameters, then the error

5, a method can only have a variable length parameter

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.