Number of variable parameters for Java methods

Source: Internet
Author: User

Many Java newcomers will ask a question when they see the code below: What are the three dots in the Dealarray method?

public class Testvarargus {public static void Dealarray (int ... intarray) {} public static void Main (String args[]) {}}

Public class testvarargus {      public static void  dealarray (int ...  intarray) {          for  (int i :  Intarray)               system.out.print ( i + " ");                     system.out.println ();      }             public static void main (String args[ ]) {          dealarray ();           dealarray (1);           Dealarray (1, 2, 3);       }  }

Output:

1 1 2 3

Through the invocation in the main method, it can be seen that this variable parameter can be either no parameter (NULL parameter) or indefinite length. As you can see here, this indefinite parameter is actually quite similar to the set of parameters. In fact, that's exactly what happened. The compiler will quietly convert the last parameter to an array parameter and make a tick in the compiled class file, indicating that this is a variable number of arguments. Please look at the code:

Dealarray ();  Dealarray (int[] intarray{}); Dealarray (1);  Dealarray (int[] intarray{1}); Dealarray (1, 2, 3); Dealarray (int[] intarray{1, 2, 3});

Speaking of which, it is possible to verify that the variable parameter is not an array class parameter using overloading? Look at the code:

Public class testvarargus {      public static void  dealarray (int ...  intarray) {          for  (int i :  Intarray)               system.out.print ( i + " ");                     system.out.println ();      }             public static void dealarray (int[]  Intarray) {//will have Duplicate method dealarray (int[])  in type testvarargus error            for  (Int i : intarray)                system.out.print (i + " ");                      System.out.println ();      }             public static void main (string args[]) {           dealarray ();            dealarray (1);           dealarray (1, 2, 3);       }  }

As you can see from the above code, these two methods are conflicting and cannot be overloaded, so the two methods should be implemented in the same way. Here, let's do an interesting experiment:

Variable parameters are compatible with the public class testvarargus {      public  of array class parameters Static void dealarray (int ...  intarray) {          for  (int i :  Intarray)               system.out.print ( i + " ");                     system.out.println ();      }             public static void main (String args[ ]) {          int[] intarray = {1, 2,  3};                     dealarray (intarray);   //compile, normal run       }  } 

Array class parameters are incompatible with mutable parameters public class Testvarargus {public static void Dealarray (int[] intarray) {for (int I:intar                    Ray) System.out.print (i + "");      System.out.println ();  } public static void Main (String args[]) {Dealarray (1, 2, 3); Compilation Error}}

As can be seen from the above two pieces of code, mutable parameters are compatible with array class parameters, but array class parameters are not compatible with mutable parameters. In fact, for the second piece of code, the compiler does not know what mutable immutable, in its view, the need to define a dealarray (int, int, int) class method. Therefore, it is not possible to match the Dealarray method of array class parameters naturally.

Now that the Java method receives the mutable parameters, let's look at the following code:

The variable parameter type must be the last item in the parameter list because the priority of the fixed-length parameter is greater than the indeterminate length parameter public class Testvarargus {public static void Dealarray (int count, int ... int                Array) {} public static void Dealarray (int ... intarray, int count) {//Compile error, variable parameter type should be the last item in the parameter list public static void Main (String args[]) {}}

This code shows that the variable parameter type must be the last item in the parameter list, not the length parameter. It is estimated that you will think of a word "priority". Because there is no exact description, just one such rule, here can borrow the word "priority" to understand, see the following code:

public class Testvarargus {public static void Dealarray (int ... intarray) {System.out.println ("1");      } public static void Dealarray (int count, int count2) {System.out.println ("2");      } public static void Main (String args[]) {Dealarray (1, 2); }  }

It is estimated that the code is output 2, not 1. Remember: to match the fixed length method, the method is preferred. The overloaded method that contains indeterminate parameters is the last to be selected.


Finally, we all know that the Main method parameter is an array type, then it can also be changed to variable parameter type. Try it and see if there is a compile error.


Number of variable parameters for Java methods

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.