On the variable parameter _java in Java

Source: Internet
Author: User

Variable parameters

A variable parameter is a method that can receive any number of arguments! For example: Fun (), fun (1), Fun (1,1), Fun (1,1,1). You may think that this is a method overload, but this is not an overload, you think how many methods overload can overload, and the fun () method can pass any number of parameters, you can overload so many methods?

2.1 Defining variable parameter methods

Public voidfun (int ... arr) {}

The parameter type of the above method fun () is int ..., where "..." is not an ellipsis, but rather a way of defining the parameter type. Parameter arr is the variable parameter type. You can interpret the above code as: public void Fun (int[] arr).

public int sum1 (int[] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length i++) {
      sum + + arr[i];
    }
    return sum;
  }

public int sum2 (int ... arr) {
    int sum = 0;
    for (int i = 0; i < arr.length i++) {
      sum + + arr[i];
    }
    return sum;
  }

You might think, "int[]" and "int ..." is no different, just "int ..." is a new way of defining an array parameter. Then I should congratulate you! Yes, that's the right thing to understand! Note, however, that only int can be used in the method's formal parameters ... To replace int[].

2.2 Calling a method with variable parameters

Calls to Sum1 () and sum2 () two methods:

SUM1 (New int[]{1,2,3});
Sum2 (New int[]{1,2,3});

It doesn't look any different! But there is another way to invoke sum2:

Sum2 ();

SUM2 (1);

SUM2 (1,2);

Sum2 (1,2,3);

This may seem like using any number of arguments to invoke the Sum2 () method, which is the benefit of invoking the method with variable parameters.

2.3 Compiler "Two processing"

The result of the compiler's "two machining" definition of the Sum2 method is:

public int sum2 (int[] arr) {
    int sum = 0;
    for (int i = 0; i < arr.length i++) {
      sum + + arr[i];
    }
    return sum;
  }

Change "int ..." to "int[]" type.

The compiler's two load results for the Sum2 method call are:

SUM2 (new int[0]);
SUM2 (New int[] {1});
SUM2 (New int[] {1, 2});
SUM2 (New int[] {1, 2, 3});

Conclusion: the variable parameter is actually an array type, but it is convenient to invoke the method, and the compiler helps us to pass multiple arguments to the formal parameter in an array.

2.4 Limitations of the variable parameter method

L A method can only have one variable parameter at most;

L The variable parameter must be the last parameter of the method.

The above is a small series for everyone to talk about the variable parameters in Java all the content, I hope to help you, a lot of support cloud Habitat Community ~

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.