Instance analysis of variable parameter types for Java methods

  • 2020-04-01 03:28:01
  • OfStack

Variable-parameter types in Java methods are a very important concept and are widely used. This paper analyzes this problem in the form of an example. The details are as follows:

In general, many Java novices ask the following question when they see this code: what are those three dots in the dealArray method?


public class TestVarArgus { 
  public static void dealArray(int... intArray){ 
     
  } 
   
  public static void main(String args[]){ 
     
  } 
} 

This is what this article is about: variable parameter types, also known as indeterminate parameter types. I'm going to call it varargus, which is the variable argument type. As you can see directly from its name, the number of arguments it receives is variable. Okay, so let's call this method first. Look at the code and output:


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 call to the main method, you can see that the variable parameter can be either no parameter (empty parameter) or variable length. If you look at this, you can see that this variable length parameter is actually very similar to an array parameter. In fact, it is. The compiler quietly converts this last parameter to an array parameter and marks the compiled class file as a method with a variable number of arguments. See the code:


dealArray(); //dealArray(int[] intArray{}); 
dealArray(1); //dealArray(int[] intArray{1}); 
dealArray(1, 2, 3); //dealArray(int[] intArray{1, 2, 3}); 

So, with that said, you can verify that this mutable argument is an array class argument, right? 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){//There will be an error duplicating method dealArray(int[]) in type TestVarArgus
    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, the two methods are in conflict and cannot be overloaded. Here's another interesting experiment:

Code 1:


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 and run normally
  } 
} 

Code 2:


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
  } 
} 

It can be seen from the above two pieces of code that 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 knows nothing about mutable immutable, and in its view, it needs to define a method of the dealArray(int, int, int) class. So, of course, there is no way to match the dealArray method for an array class parameter.

Now that the Java method accepts mutable arguments, let's take a look at the following code:


public class TestVarArgus { 
  public static void dealArray(int count, int... intArray){ 
     
  } 
   
  public static void dealArray(int... intArray, int count){//Compile error. The variable parameter type should be the last item in the parameter list
     
  } 
   
  public static void main(String args[]){ 
      
  } 
} 

This code illustrates that the variable-parameter type must be at the end of the parameter list, not before a fixed-length parameter. The word "priority" probably comes to mind. Since there is no specific explanation, just such a 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); 
  } 
} 

If you post code, you probably know that the output is 2, not 1. Here's what to remember: If a method with a fixed length can be matched, it is preferred to match that method. The overloaded method with an indefinite parameter is the last to be selected .

Finally, everyone knows The argument to the main method is an array type, so it can be changed to an indeterminate parameter type . Try it out and see if there are any compilation errors.

I believe that this article has certain reference value to the study of Java programming.


Related articles: