Variable Parameter types of Java Methods

Source: Internet
Author: User

When many new Java users see the following code, they will ask a question: what are the three dots in the dealArray square method?

Public class TestVarArgus {

Public static void dealArray (int... intArray ){}

Public static void main (String args []) {}

}

This is the main character of today's blog: variable parameter types, also known as variable parameter types. The abbreviation is varargus, Which is variable argument type. It can be seen from its name that the number of parameters received by this method is not fixed. Now, call this method. See the code and output:

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 []) {

DealArray ();

DealArray (1 );

DealArray (1, 2, 3 );

}

}

Output: 1 1 2 3

 

By calling the main method, we can see that this variable parameter can be neither a parameter (null parameter) nor a variable length. We can see that the variable length parameter is similar to the array parameter. In fact, this is indeed the case. The compiler will quietly convert the last form parameter into an array form parameter and generate a mark in the compiled class file, indicating that this is a method with variable real parameters. See the Code:

DealArray (); // dealArray (int [] intArray {});

DealArray (1); // dealArray (int [] intArray {1 });

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

Here, let's verify whether the variable parameter is an array parameter? Check 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) {// Duplicate method dealArray (int []) in type TestVarArgus Error

For (int I: intArray)

System. out. print (I + "");

System. out. println ();

}

Public static void main (String args []) {

DealArray ();

DealArray (1 );

DealArray (1, 2, 3 );

}

}

 

From the code above, we can see that these two methods conflict and cannot be reloaded. Here, let's start an interesting experiment:

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); // compiled to run normally

}

}

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 []) {

DealArray (1, 2, 3); // compilation Error

}

}

 

From the above two sections of code, we can see that variable parameters are compatible with array parameters, but array parameters are not compatible with variable parameters. In fact, for the second paragraph of code, the compiler does not know what can be changed and immutable. In its view, a dealArray (int, int, int) class method needs to be defined. Therefore, the dealArray method of array parameters cannot be matched. Since the Java method receives variable parameters, let's take a look at the following code:

Public class TestVarArgus {

Public static void dealArray (int count, int... intArray ){}

Public static void dealArray (int... intArray, int count) {// an error is reported during compilation. The variable parameter type should be the last of the parameter list}

Public static void main (String args []) {}

}

 

This Code shows that the variable parameter type must be the last item in the parameter list and cannot be placed before the fixed-length parameter. It is estimated that you will think of a word "priority ". Because there is no definite explanation, it is just such a provision. here we can use the word "Priority" to understand it. Please refer to 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 );

}

}

 

It is estimated that the code is output 2, not 1. Remember: If you can match a method with a fixed length, the method will be matched first. The overload method with variable parameters is finally selected.

Finally, we all know that the parameter of the main method is an array type, so it can be changed to an indefinite parameter type. Try it and see if there are any compilation errors.

 

From: http://blog.csdn.net/justinavril/archive/2009/10/15/4674305.aspx

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.