Key Java Technology (2) variable parameters of syntax

Source: Internet
Author: User

1.1.variable Parameters

Variable parameter refers to the number of arguments in a function that is undefined when the function is defined, and the caller can pass in a different number of arguments.

mutable parameters are defined in the following way, and mutable parameters can only be the last parameter of a function.

type ... Parameter name

mutable parameters are compiled into arrays, so they can be used as an array in a function.

/*** @Title: variableparametertest.java* @Package com.test.javatechnology* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:21:44*@versionV1.0*/ Packagecom.test.javatechnology;/*** @ClassName: variableparametertest* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 19, 2017 pm 8:21:44**/ Public classVariableparametertest {Static  voidTest (String name,int.. args) {inti; SYSTEM.OUT.PRINTLN (name);  for(i=0;i<args.length;i++) {System.out.println (args[i]);}} /** * @paramargs*/ Public Static voidMain (string[] args) {Test ("Length is 0"); Test ("Length is 1", 10); Test ("Length is 2", 10, 20); } } 

Variable parameters can be used as arrays, but if the last parameter is defined as an array, you cannot call this function

Used as a variable parameter.

Static  void Test (String  int[] args) {}

This function cannot be used in this way, or it will compile incorrectly.

Test ("length is 0");Test ("Length is 1", andtest ("length is 2", 10, 20);

Key Java Technology (2) variable parameters of syntax

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.