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:
- The mutable parameter must be the last variable of the function, that is, at the end of the argument list
- Variable parameters with ... Said
- 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