New Java Features--variable parameters

Source: Internet
Author: User

Variable parameters:

1. Parameters can be passed without the limit of the number of arguments, all the parameters are stored in the form of an array.

2. Variable parameter definition Format: Return value type method name (Type ... Parameter name) {}, Note: ... Between the variable type and the variable name

3. The definition of an optional variable must be placed after the parameter list.

Why does the definition of a mutable parameter have to be placed behind the parameter list?

In the absence of a mutable parameter, Java is passing the accepted parameter type to an array, in the form of an array, traversing the method, requiring several elements to add several elements (public static void fun (int a[]) {}). Later there are parameter variables, although the form is changed, but the internal parameters are stored in the form of an array (public static void fun (int ... a)). ... What follows is an array that passes the arguments to the array and saves them, so if the definition of the mutable parameter is not placed after the argument list, the argument is not passed to the array as an array.

public class Newdemo02{public static void Main (String args[]) {System.out.print ("Do not pass parameter (fun ()):"), fun ();// Do not pass parameter System.out.print ("\ n pass a parameter (fun (1)):"), Fun (1),//pass a parameter System.out.print ("\ n pass five arguments (fun (1,2,3,4,5)):"); 1,2,3,4,5);} public static void Fun (int ... arg) {//variable parameter for (int x:arg) {//Use foreach output output System.out.print (x + ",");}};

  

New Java Features--variable 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.