Example analysis of variable parameter types for Java methods _java

Source: Internet
Author: User

Variable parameter types in Java methods are a very important concept and have a very wide range of applications. This article analyzes it in the form of an example. Specifically as follows:

In general, many Java beginners will ask a question when they see the following code: What are the three small dots in the Dealarray method?

public class Testvarargus {public 
  static void Dealarray (int ... intarray) {} public 
   
  static void main (String a Rgs[]) { 
     
  } 
} 

This is the problem that this article discusses: variable parameter types, also called indeterminate parameter types . English abbreviation is Varargus, restore is variable argument type. By its name can be very straightforward to see that this method in the receiving parameters, the number is variable. Well, now let's call this method first. See Code and output:

public class Testvarargus {public 
  static void Dealarray (int ... intarray) {for 
    (int i:intarray) 
      System.ou T.print (i + ""); 
     
    System.out.println (); 
  } 
   
  public static void Main (String args[]) { 
    dealarray (); 
    Dealarray (1); 
    Dealarray (1, 2, 3); 
  } 
} 

Output:

1
1 2 3

From the call in the main method, you can see that the variable parameter can be either without parameters (Null parameters) or indefinite length. As you can see here, this indefinite parameter is quite similar to the array parameters. In fact, it is true. The compiler silently converts this last parameter into an array parameter and makes a notation in the compiled class file, indicating that it is a method with 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}); 

So here, let's verify that the variable parameter is the array class argument. 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 for the duplicate method Dealarray (int[]) in type Testvarargus 
    (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 conflicting and cannot be overloaded. Here, let's do an 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); Through compilation, normal operation 
  } 
} 

Code 2:

public class Testvarargus {public 
  static void Dealarray (int[] intarray) {for 
    (int i:intarray) 
      System.ou T.print (i + ""); 
     
    System.out.println (); 
  } 
   
  public static void Main (String args[]) { 
    Dealarray (1, 2, 3);//Compile Error 
  } 
} 

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

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

public class Testvarargus {public 
  static void Dealarray (int count, int ... intarray) {} public 
   
  static void DEA Larray (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 argument list, not the one in front of the fixed-length argument. Guess you'll think of a word "priority". Because there is no exact explanation, just such a provision, here you can use 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); 
  } 
} 

The code is posted to estimate that it is output 2 instead of 1. Here you need to remember that if you can match a fixed-length method, then the method is preferred. The overloaded method with indefinite parameters is the last selected .

Finally, we all know that the Main method parameter is an array type, then it can also be changed to an indefinite parameter type . Try it and see if there are any compiler errors.

It is believed that this article has some reference value to the learning of Java programming.

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.