Java mutable parameters

Source: Internet
Author: User

We might want to do something about the parameters in one method in some particular requirement environment, and the parameters in these methods are not specified, so what do we do? In fact, Java has considered this situation for us, that is, the use of variable parameters

Features of variable parameters:

1: Can only appear at the end of the parameter list

2: ... Between the variable type and the variable name, there are no spaces before and after it can be

3: When you call a variable parameter method, the editor creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body


Write a demo.

public class Demo {
public static void Main (string[] args) {
System.out.println (Add (2,3,4,5));
}
static int Add (int a,int ... x) {
int sum = A;
for (int i=0;i<x.length;i++) {
Sum+=x[i];
}
return sum;
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java mutable parameters

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.