Java Basics _ mutable parameters

Source: Internet
Author: User

Since JAVA1.5, variable parameters have appeared in Java, which is a case where multiple indeterminate elements of the same type are operated in the same class.

mutable parameters are somewhat similar to overloaded concepts, but the mutable parameters are implicitly converted into an array for processing.

For example: Summing multiple numbers, but not knowing how many such numbers are in particular.

Conditions of Use:

  1. The mutable parameter must be the last variable of the function, that is, at the end of the argument list
  2. Variable parameters with ... Said
  3. When a method of a mutable parameter is called, the compiler creates an array for the mutable parameter implicitly, accessing the mutable parameter as an array in the method body


/*** Multiple Parameter summation *@authorAdministrator **/ Public classVariableparameter {/**     * @paramargs*/     Public Static voidMain (string[] args) {System.out.println ("Add (+/-) =" + Add ()); System.out.println ("Add (1,2,4,5,6,7,8) =" + Add (1,2,4,5,6,7,8)); }        /*** Multiple Number summation *@paramFirst Addend *@paramothers the rest of Addend *@returnThe result of Addend summation*/     Public Static intAddintFirstint... others) {        intsum =First ;  for(inti = 0; i < others.length; i++) {sum+=Others[i]; }        returnsum; }}

Java Basics _ 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.