Because in the previous process of learning Java, there are variable parameters involved, but not too deep to learn. So recently I have found some relevant information, want to deepen understanding. The following is the content of learning to organize.
When you define a method after JDK1.5, you can use an indeterminate number of parameters of the same type.
When you define a method with a mutable parameter, the method actually puts the indeterminate number of arguments into an array implicitly, and then the method takes the elements out of the array to use.
That is, the method uses variable arguments, and the methods used in JDK1.4 are treated the same way when using array parameters, all of which are taken out of the array elements to use.
First, the definition method:
Return value method name (parameter type ...) Variable parameter name)
1 void Method (String...args) {2 3 }
Second, call method:
1 classVarargstest {2 Public Static voidmethod (String ... str) {3 4 }5 6 Public Static voidMain (string[] args) {7Method ("");8Method ("AAA");9Method ("AAA", "BBB");Ten } One}
Third, the use of precautions:
1. There can be only one variable parameter in a method, and the mutable parameter must be in the last position of the parameter list. Otherwise, the virtual machine cannot determine which parameter the parameter value in the passed-in method belongs to.
2. The variable parameter can be treated as an array directly inside the method:
1 void Print (String...args) {2 for (String Str:args) {3 System.out.println (str); 4 }5 }
3. If there are two methods (overloads) of the same name with fixed and mutable parameters in the class, the arguments that invoke the method can also match their argument list, and the method that uses the fixed parameter is preferred.
1 classVarargstest {2 3 Public voidprint (String ... args) {4 for(inti = 0; i < args.length; i++) {5 System.out.println (Args[i]);6 }7 }8 9 Public voidprint (String test) {TenSystem.out.println ("----------"); One } A - Public Static voidMain (string[] args) { -Varargstest test =Newvarargstest (); theTest.print ("Hello");//The result is---------- - } -}
4. Avoid method overloads with variable parameters.
The following are the wrong:
1 classVarargstest {2 3 Public voidprint (String ... args) {4 for(inti = 0; i < args.length; i++) {5 System.out.println (Args[i]);6 }7 }8 9 Public voidprint (String test,string...args) {Ten System.out.println (test); One } A - Public Static voidMain (string[] args) { -Varargstest test =Newvarargstest (); theTest.print ("Hello", "hi");//the compilation does not pass because the Java virtual machine does not know which method to call. - } -}
Here are some easy mistakes to make:
1 classVarargstest {2 3 Public voidprint (String test,integer ... is) {4 for(inti = 0; i < is.length; i++) {5 System.out.println (Is[i]);6 }7 }8 9 Public voidprint (String test,string...args) {Ten for(inti = 0; i < args.length; i++) { One System.out.println (Args[i]); A } - } - the Public Static voidMain (string[] args) { -Varargstest test =Newvarargstest (); - //when the Print method is called, only one argument is passed, which means that the argument passed to the mutable parameter is null. - //The variable-length parameter type cannot be judged from null at this point, and the Java Virtual machine does not know which method to call and an error occurs. + //So in this case, if the passed argument is empty, you must confirm the type of the argument by defining the variable as empty. -String str =NULL; +Test.print ("Hello", str); A //test.print ("Hello", null); //the compilation does not pass because the Java virtual machine does not know which method to call. at } -}
5. A method of the form parameter list of an array can be used in a subclass to override a method with variable parameters in the parent class.
The reason is also known in the description at the beginning of the article, in fact the method is to treat the mutable parameter as an array, which results in a successful rewrite.
This should be a special case, because the parameter list of the two methods is different and can be overridden.
Here is a specific example:
1 classVarargstest {2 3 Public Static voidMain (string[] args) {4 5 //sub-upward transformation. 6Base base =NewSub ();7Base.print ("Hello");//The compilation succeeds here, because the type promotion is involved, and when the method is called, the argument is based on the parameter list of the parent class to determine whether the argument is compliant,8 //and the implementation is the specific content of the subclass method, output sub......test. 9 Ten //Sub has not been transformed. OneSub Sub =NewSub (); ASub.print ("Hello");//The compilation is unsuccessful here, because the method called by the sub itself, the parameter does not conform to the parameter list in the method. - } - } the - //Parent Class - classBase { - voidprint (String ... args) { +System.out.println ("Base......test"); - } + } A at //sub-class - classSubextendsBase { - - voidprint (string[] args) { -System.out.println ("Sub......test"); - } in}
6. When calling a method with a mutable parameter with a parameter type of object, the pass argument is an array of a single base data type, and a single other type of array is passed, and the method has different ways of handling it.
Explain the processing method separately:
(1) An array of basic data types is a subclass of object, and when it is passed to a method as an argument, the method treats the array as an object, then puts the object into a object[] array, and then takes the element out of the object[] array. It can be seen that this is handled in the same way as if the arguments passed into the array are not handled in the method.
(2) Other types of arrays are also subclasses of object, and when passed to a method as an argument, the method considers it to be a object[] array and will not be loaded into the new object[] array, but instead is taken out of the element directly from the object[] array.
As you can see from the above, the key point of the difference processing is whether the array is considered an object object or an objuect[] array.
Based on the key points, when we pass the arguments of other types of arrays, if we want the whole array to participate in the method as an object, it can be implemented in two ways.
(1) The other type of array is loaded into a object[] array (into a two-dimensional array), so that when the method receives the argument, it will be treated as a object[] array, and then directly from the object[] array to take out the elements to use, This allows you to take out the other types of arrays we define.
(2) The other types of arrays are strongly turned into object objects (using (Object)), so that when the method receives the arguments, the arguments are treated as object objects, loaded into a new object[] array, and then removed from the array using the element, This allows you to take out object objects, which are other types of arrays that we define.
Here is a concrete example:
1 classVarargstest {2 3 Public Static voidMain (string[] args) {4 5 //an array of basic data types. 6 int[] arr = {1,2,4};7 //an array of strings. 8String[] arr1 = {"A", "B", "C"};9Test T =NewTest ();TenT.print (arr);//The result prints the hashcode address of the int array. OneT.print (ARR1);//The result prints each element in the string array. AT.print ((Object) (arr1));//The result prints the hashcode address of the string array. - } - } the - classTest { - - //the variable parameter type is object. + voidprint (Object ... args) { - for(Object Obj:args) { + System.out.println (obj); A } at } -}
About the variable parameters need to pay attention to the place is almost, looking for information before thought is relatively simple things. The results only found that there are a lot of things to pay attention to, really learning ah ...
Java Fundamentals: Usage Considerations for variable parameters (VARAGRS)