Depth analysis of the difference between Java and variable parameter types and arrays

Note: The variable parameter type is a new feature in version jdk1.5, jdk1.0 there is an array type.

This article describes the type of variable parameters Java methods, for example by variable parameter type in Java a more in-depth analysis, a friend in need can refer to.

 

The variable parameter types Java method is a very important concept, has a very wide range of applications. This paper focuses on instances of this form of analysis. details as follows:

In general, many beginners see the following Java code that they will ask a question: What dealArray method in the three small little bit of ah?

 1     public class TestVarArgus {
 2      
 3         public static void dealArray(int... intArray) {
 4      
 5         }
 6      
 7         public static void main(String args[]) {
 8      
 9         }
10     }

This is the question to be discussed in this article: variable parameter type, also known as variable parameter type. English abbreviation is varargus, reducing what is the variable argument type. It can be directly seen by its name, this method when it receives a parameter, the number is uncertain. So good, and now the first to call this method. Look at the code and output:

 1 public class TestVarArgus {
 2     public static void dealArray(int... intArray) {
 3         for (int i : intArray)
 4             System.out.print(i + " ");
 5  
 6         System.out.println();
 7     }
 8  
 9     public static void main(String args[]) {
10         dealArray();
11         dealArray(1);
12         dealArray(1, 2, 3);
13     }
14 }

Output:

1
1 2 3

By main method in the call, you can see the variable parameter can be either no parameter (null parameter), it can be of variable length. See here estimate can understand, this long and indefinite parameters actually quite like array parameters. In fact, it really is such a thing children. The compiler will quietly this last parameter into a parameter array, and for at compile the class file on a mark, indicating that this is a real number variable parameter method. Look at the code:

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

Here, you can verify it and see is not the argument is an array class variable parameters? Look at the code:

 1 public class TestVarArgus {
 2     public static void dealArray(int... intArray) {
 3         for (int i : intArray)
 4             System.out.print(i + " ");
 5  
 6         System.out.println();
 7     }
 8  
 9     public static void dealArray(int[] intArray) {
10         // 会有Duplicate method dealArray(int[]) in type TestVarArgus的错误
11         for (int i : intArray)
12             System.out.print(i + " ");
13  
14         System.out.println();
15     }
16  
17     public static void main(String args[]) {
18         dealArray();
19         dealArray(1);
20         dealArray(1, 2, 3);
21     }
22 }

 

As can be seen from the above code, these two methods are in conflict, it is not overloaded. Here, come back to do an interesting experiment:

Code 1:

. 1  public  class TestVarArgus {
 2      public  static  void dealArray ( int ... intArray) {
 . 3          for ( int I: intArray)
 . 4              of System.out.print (I + "" );
 . 5   
. 6          System.out.println ();
 . 7      }
 . 8   
. 9      public  static  void main (String args []) {
 10          int [] = {intArray. 1, 2,. 3 };
 . 11   
12 is          dealArray (intArray); // compile, running 
13      }
14 }

Code 2:

 1 public class TestVarArgus {
 2     public static void dealArray(int[] intArray) {
 3         for (int i : intArray)
 4             System.out.print(i + " ");
 5  
 6         System.out.println();
 7     }
 8  
 9     public static void main(String args[]) {
10         dealArray(1, 2, 3);// 编译错误
11     }
12 }

This can be seen from the above two pieces of code out of the variable parameters are compatible with an array of class parameters, but the parameters can not be compatible array class variable parameters. In fact, for the second paragraph of the code, the compiler does not know what the variable immutable, in its view, need to define a method of the class dealArray (int, int, int). So, naturally, unable to match dealArray method of the Array class parameters.

Since Java method accepts variable parameters, then the next we will look at the following code:

. 1  public  class TestVarArgus {
 2      public  static  void dealArray ( int COUNT, int ... intArray) {
 . 3   
. 4      }
 . 5   
. 6      public  static  void dealArray ( int ... intArray, int COUNT) {
 . 7          // compile error, a variable parameter type would be the last one of the parameter list 
. 8   
. 9      }
 10   
. 11      public  static  void main (String args []) {
 12 is   
13 is      }
 14 }

Code illustrates, the variable parameter type argument list must be the last one, but not in front of the fixed length parameter. It estimated that you will think of the word "priority." Because there is no exact explanation, but such a provision, where you can borrow "priority" is the word to understand it, see the following code:

 1 public class TestVarArgus {
 2     public static void dealArray(int... intArray) {
 3         System.out.println("1");
 4     }
 5  
 6     public static void dealArray(int count, int count2) {
 7         System.out.println("2");
 8     }
 9  
10     public static void main(String args[]) {
11         dealArray(1, 2);
12     }
13 }

Code stickers estimated all know is the output 2, instead of 1. We should keep in mind: to fixed-length matching method, then match the priority method. Overloaded methods that contain variable parameter is the last to be chosen.

Finally, we all know that the main method parameter is an array type, then it can actually change the variable parameter type. Try it, see if there is a compilation error.

I believe the paper has some reference value for all of us to learn Java programming.


Original Reference: https: //blog.csdn.net/baidu_37107022/article/details/78033591

Guess you like

Origin www.cnblogs.com/zhuitian/p/12274443.html