Java: Methods with variable number of parameters

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?

[Java] view plaincopy

    1. PUBLIC CLASS TESTVARARGUS {  
    2. Span style= "font-size:16px" >    public static void dealarray (int ...  intarray) {  
    3.            
    4.     }  
    5.       
    6.     public static void main (String args[]) {   
    7.            
    8.     }  
    9. }  

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

  1. public class Testvarargus {
  2. public static void Dealarray (int ... intarray) {
  3. for (int i:intarray)
  4. System.out.print (i + "");
  5. System.out.println ();
  6. }
  7. public static void Main (String args[]) {
  8. Dealarray ();
  9. Dealarray (1);
  10. Dealarray (1, 2, 3);
  11. }
  12. }

Output:

[Java] view plaincopy

    1. 1
    2. 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

    1. Dealarray (); Dealarray (int[] intarray{});
    2. Dealarray (1); Dealarray (int[] intarray{1});
    3. 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

  1. public class Testvarargus {
  2. public static void Dealarray (int ... intarray) {
  3. for (int i:intarray)
  4. System.out.print (i + "");
  5. System.out.println ();
  6. }
  7. public static void Dealarray (int[] intarray) {//will have duplicate method Dealarray (int[]) in type Testvarargus error
  8. for (int i:intarray)
  9. System.out.print (i + "");
  10. System.out.println ();
  11. }
  12. public static void Main (String args[]) {
  13. Dealarray ();
  14. Dealarray (1);
  15. Dealarray (1, 2, 3);
  16. }
  17. }

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

  1. public class Testvarargus {
  2. public static void Dealarray (int ... intarray) {
  3. for (int i:intarray)
  4. System.out.print (i + "");
  5. System.out.println ();
  6. }
  7. public static void Main (String args[]) {
  8. Int[] Intarray = {1, 2, 3};
  9. Dealarray (Intarray); Compile, run normally
  10. }
  11. }

[Java] view plaincopy

  1. public class Testvarargus {
  2. public static void Dealarray (int[] intarray) {
  3. for (int i:intarray)
  4. System.out.print (i + "");
  5. System.out.println ();
  6. }
  7. public static void Main (String args[]) {
  8. Dealarray (1, 2, 3); Compile error
  9. }
  10. }

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

  1. public class Testvarargus {
  2. public static void Dealarray (int count, int ... intarray) {
  3. }
  4. public static void Dealarray (int ... intarray, int count) {//Compile error, variable parameter type should be the last item in the argument list
  5. }
  6. public static void Main (String args[]) {
  7. }
  8. }

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

  1. public class Testvarargus {
  2. public static void Dealarray (int ... intarray) {
  3. System.out.println ("1");
  4. }
  5. public static void Dealarray (int count, int count2) {
  6. System.out.println ("2");
  7. }
  8. public static void Main (String args[]) {
  9. Dealarray (1, 2);
  10. }
  11. }

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

Related Article

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.