Many Java newcomers will ask a question when they see the code below: What are the three dots in the Dealarray method?
[Java] view plaincopy
- PUBLIC CLASS TESTVARARGUS {  
- Span style= "font-size:16px" > public static void dealarray (int ... intarray) {
-
- }
-       
- public static void main (String args[]) {
-
-     }  
- }  
A variable parameter type, also known as an indeterminate parameter type. English abbreviation is Varargus, restore is variable argument type. By its name can be very straightforward to see, this method in the receiving parameters, the number is variable. Well, let's call this method now. Take a look at the code and output:
[Java] view plaincopy
- 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:
[Java] view plaincopy
- 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:
[Java] view plaincopy
- Dealarray (); Dealarray (int[] intarray{});
- Dealarray (1); Dealarray (int[] intarray{1});
- Dealarray (1, 2, 3); Dealarray (int[] intarray{1, 2, 3});
Speaking of which, then you can check to see if this variable parameter is an array class parameter?
[Java] view plaincopy
- 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. Here, let's do an interesting experiment:
[Java] view plaincopy
- 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[]) {
- Int[] Intarray = {1, 2, 3};
- Dealarray (Intarray); Compile, run normally
- }
- }
[Java] view plaincopy
- 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 (1, 2, 3); Compile 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:
[Java] view plaincopy
- public class Testvarargus {
- public static void Dealarray (int count, int ... intarray) {
- }
- public static void Dealarray (int ... intarray, int count) {//Compile error, variable parameter type should be the last item in the argument 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:
[Java] view plaincopy
- 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.
Java: Methods with variable number of parameters