The variable-length parameter (varargs) is provided in the JAVA5, which means that the number of parameters can be used in the method definition, with different number of parameters to invoke for the same method, such as print ("Hello");p rint ("Hello", "Lisi");p rint ("Hello", "Zhang San", "Alexia"); The following describes how to define variable-length parameters and how to use variable-length parameters.
1. Definition of variable length parameters
use ... Represents variable-length parameters , such as
Print (String ... args) {
...
}
In a method with variable length parameters, parameters can be used as arrays, for example, all parameter values can be looped out.
Print (String ... args) {
for (String Temp:args)
SYSTEM.OUT.PRINTLN (temp);
}
2. Invocation of a variable-length parameter method
Any number of arguments can be given at the time of invocation and no arguments can be given, for example:
Print ();
Print ("Hello");
Print ("Hello", "Lisi");
Print ("Hello", "Zhang San", "Alexia")
3. Rules for the use of variable length parameters
3.1 If the method is called, if it can match the method of the fixed parameter and can match the method of variable length parameter, then the method of fixed parameter is selected. Look at the output of the following code:
The package com;//is used here for static import of import static Java.lang.system.out;public class Varargstest {public void print (String ... args) {for (int i = 0; i < args.length; i++) { out.println (args[i]); } } public void print (String test) { out.println ("----------"); } public static void Main (string[] args) { varargstest test = new Varargstest (); Test.print ("Hello"); Test.print ("Hello", "Alexia");} }
3.2 If the method to be called can match two mutable parameters, an error occurs, such as the following code:
The package com;//is used here for static import of import static Java.lang.system.out;public class VarArgsTest1 {public void print (String ... args) {for (int i = 0; i < args.length; i++) { out.println (args[i]); } } public void print (String test,string...args) { out.println ("----------"); } public static void Main (string[] args) { VarArgsTest1 test = new VarArgsTest1 (); Test.print ("Hello"); Test.print ("Hello", "Alexia");} }
For the above code, the two calls in the Main method cannot be compiled, because the compiler does not know which method call to choose, as follows:
3.3 A method can have only one variable-length parameter, and the variable-length parameter must be the last parameter of the method
The following two method definitions are incorrect.
public void Test (String ... strings,arraylist list) {
}
public void Test (String ... strings,arraylist. List) {
}
4. Specification for the use of variable length parameters
4.1 Avoid method overloads with variable-length parameters: such as 3.1, the compiler knows how to call, but people are prone to fall into the trap of calling and misunderstanding
4.2 Do not let null values and Null values threaten the variable length method, as shown in 3.2, to illustrate the invocation of a null value, give an example again:
Package Com;public class VarArgsTest1 {public void print (String test, Integer ... is) {} public void print (St Ring Test,string...args) { } public static void Main (string[] args) { VarArgsTest1 test = new VarArgsTest1 (); test.print ("Hello"); Test.print ("Hello", null);} }
You will find that two call compilations do not pass:
Because the two methods are matched, the compiler does not know which one to choose, so the error, there is also a very bad coding habit, that is, the caller hides the actual parameter type, which is very dangerous, not only the caller needs to "guess" which method to call, and the callee may also produce internal logic chaos situation. For this example, the following modifications should be made:
public static void Main (string[] args) { VarArgsTest1 test = new VarArgsTest1 (); string[] STRs = null; Test.print ("Hello", STRs); }
4.3 Overwrite the variable length method also must obey the rules
Let's take a look at an example and guess if the program can be compiled by:
Package Com;public class VarArgsTest2 { /** * @param args * /public static void main (string[] args) {
//TODO auto-generated Method stub //Up transformation Base base = new Sub (); Base.print ("Hello"); Do not transform sub Sub = new sub (); Sub.print ("Hello");} } Base class class Base { void print (String ... args) { System.out.println ("Base......test");} } Subclass, Overwrite parent class method class Sub extends Base { @Override void print (string[] args) { System.out.println ("Sub .... Test ");} }
The answer is, of course, the compilation does not pass, is it strange?
The first one can be compiled through, which is why? In fact, the base object has been transformed into a class object sub, and the formal parameter list is determined by the parent class, and of course it does. To see what the subclass calls directly, the compiler sees the subclass overwrite the Print method of the parent class, so the print method redefined by the subclass is definitely used, even though the argument list does not match and does not run to the parent class to match, because it is no longer found, so there is a type mismatch error.
This is a special case, the override method parameter list can be different from the parent class, this violates the overwrite definition, and will cause the inexplicable error.
Here, summarize the conditions that must be met for the overwrite:
(1) The overriding method does not reduce access rights;
(2) The parameter list must be the same as the overridden method (including the display form);
(3) The return type must be the same as or subclass of the overridden method;
(4) The overriding method cannot throw a new exception, or exceeds the scope of the parent class, but can throw fewer, more limited exceptions, or not throw exceptions.
Finally, given a trap example, you should know the output:
Package Com;public class Varargstest {public static void M1 (string s, string ... ss) {for (int i = 0; i < SS.L Ength; i++) { System.out.println (ss[i]); } } public static void Main (string[] args) { M1 (""); M1 ("AAA"); M1 ("AAA", "BBB");} }
From:http://www.cnblogs.com/lanxuezaipiao/p/3190673.html
The use of variable length parameters in Java and considerations