Overloaded variable length parameter method in Java

Source: Internet
Author: User

First, the test code

 PackageCom.demo; Public classInterview { Public Static voidTestinti) {System.out.println ("Call the test (int i)"); }     Public Static voidTestint... ary) {System.out.println ("Call the test (int ... ary)");    System.out.println (ary.length); }     Public Static voidMain (string[] args) {//TODO auto-generated Method Stubtest (); Test (The); Test (10); Test (New int[]{1}); }}

Second, the output results

Call the test (int...ary)0 call thetest (int...ary)3 Call thetest (  int  i) call the test (int... ary)1

Three, analysis

1, the actual calling process of the variable parameters is actually to organize the parameters into an array, and then go in and out of the array as a formal parameter call method.

For example, test (a) is performed as follows:

       0: iconst_3       1:newarray       int       3: DUP       4: Iconst_0       5: Iconst_1       6: Iastore       7: DUP       8: iconst_1       9: iconst_2       iastore      : Thedup      :iconst_2      :iconst_3       :      iastore15:invokestatic  #41                 //  Method Test: ([I) V

It can be seen from the NewArray directive that it is the process of organizing an array.
2. The test (int i) method is overloaded with a variable length parameter test (int ... ary)

So

        Test (); // equivalent to execute test (new int[0])        Test (+/-); //// equivalent to execute test (new int[]{1,2,3})

3, Test (10) invokes the first method, stating that in the case of both test (int i) and test (int ... ary), if only one parameter is passed: Test (10), test (int i) is called.

So how do I call Test (int ... ary) in the case of passing in a parameter? Can be called by means of test (new int[]{1});

Four, the difference and the relation of the parameter of the array parameter and the variable length

Difference:

1. There are different positions: the array form parameter can appear anywhere of the formal parameter, but the variable length parameter can only appear at the end of the formal parameter list.

2, the quantity is different: the array form the formal parameter can have any plurality, but the variable length parameter may have at most one.

Contact:

A variable-length parameter is essentially an array parameter, so invoking a variable-length parameter means that you can pass in more than one parameter, or you can pass in the array parameters directly.

Reprint please indicate source: http://www.cnblogs.com/qcblog/p/7668080.html

Overloaded Variable length parameter method in Java

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.