Before Jdk5, we might want to implement a variable parameter method in this way:
PackageWww.com.thinkInJava.initializationCleanup; Public classVarArgs {Static voidPrintArray (object[] args) { for(Object Obj:args) {System.out.print (obj+ " "); } System.out.println (); } Public Static voidMain (string[] args) {PrintArray (Newobject[]{NewInteger, 23.2f, "SDFDSF",NewDouble (23.234),NewA ()}); } }classa{}
Implemented by object[] arrays.
The output is as follows:
But after Jdk5, Java finally has the characteristic of variable parameter.
syntax similar to static void PrintArray (Object ... args)
PackageWww.com.thinkInJava.initializationCleanup; Public classVarArgs {Static voidPrintArray (Object...args) { for(Object Obj:args) {System.out.print (obj+ " "); } System.out.println (); } Public Static voidMain (string[] args) {PrintArray (NewInteger (+), 23.F, "Sfsdf",NewString ("Helosdfakl"), 234,NewA ()); PrintArray ((object[])NewInteger[]{12, 23, 345, 456,NewInteger (666)}); PrintArray (); } }classa{}
The output is as follows:
The first call in the main function is visible, and this variable argument should be similar to our previous object[] method, because we pass in the ordinary different parameters, but we can use the array of foreach to traverse the element, it should be automatically converted to a series of elements into an array;
The second invocation of the main function is visible, and an array of object arrays can be passed in directly. (Here we convert an integer-referenced array to object, and the definition of the array also uses the autoboxing, that is, the normal int is automatically converted to the corresponding integer object.) )
The third invocation of the main function is visible, and for the method of variable arguments, nothing can pass through.
The following example shows that a mutable parameter is really a form of converting a parameter to an array:
PackageWww.com.thinkInJava.initializationCleanup; Public classVarargtype {Static voidf (Character. args) {System.out.print (Args.getclass ()); System.out.println ("Length" +args.length); } Static voidGint.. args) {System.out.print (Args.getclass ()); System.out.println ("Length" +args.length); } Public Static voidMain (string[] args) {f (A); f (); G (1); g (); System.out.println ("int[]:" +New int[0].getclass ()); }}
Output:
Here, the class description, which has a "[" beginning, indicates that the class is an array, and then the data type of the array is immediately followed by the parentheses, as in the following I indicates that this is a primitive data type int.
The last line of code inside the main function proves the process, outputting the getclass of an int array directly, and the output is the same as the above, proving that the mutable parameter is indeed the conversion of the element into a group.
The overloaded overload of the method can also be used with variable parameters, but may make the method very complex, it is recommended to use caution, here also gives an example:
PackageWww.com.thinkInJava.initializationCleanup; Public classOverloadingvarargs {Static voidf (Character. args) {System.out.print ("First"); for(Character C:args) System.out.print (" " +c); System.out.println (); } Static voidf (Integer ... args) {System.out.print ("Second"); for(Integer I:args) System.out.print (" " +i); System.out.println (); } Static voidf (Long ... args) {System.out.println ("Third"); } Public Static voidMain (string[] args) {f (' A ', ' B ', ' C '); F (1); F (2, 1); F (0); F (0L); //! f ();//Won ' t compile--ambiguous }}
Output Result:
As you can see here, you cannot call F () without parameters, because the compiler cannot tell which method it is. This parameterless method call does not even compile.
Java mutable parameters