[Java5 new feature] variable parameter

Source: Internet
Author: User

What is a mutable parameter

In Java's underlying content, there is an attribute to the function: overloading, if we want to complete multiple numbers to add the need, you can follow the following code to complete:

 Public classDemo { Public int Add(intAintb) {returnA + b; } Public int Add(intAintBintc) {returnA + B + C; } Public Static void Main(string[] args) {intSUM1 =NewDemo (). Add (1,2);intsum2 =NewDemo (). Add (1,2,3); System. out. println ("The result for the first time is:"+ sum1 +"; The result of the second time is:"+ sum2); }}

However, if this is done, you need to set up multiple correspondence methods, or not concise enough. We can simplify it in the way we use arrays.

 Public classDemo { Public int Add(int[] arr) {intsum =0; for(inti =0; i < arr.length;        i++) {sum + = Arr[i]; }returnSum } Public Static void Main(string[] args) {intSUM3 =NewDemo (). Add (New int[] {1,2,3,4,5}); System. out. println ("The result for the third time is:"+ sum3); }}

After the Java 5 version, the variable parameters are provided in a way that is shown in the following code:

 Public classDemo { Public int Add(int... arr) {intsum =0; for(inti =0; i < arr.length;        i++) {sum + = Arr[i]; }returnSum } Public Static void Main(string[] args) {intSUM3 =NewDemo (). Add (New int[] {1,2,3,4,5}); System. out. println ("The result for the third time is:"+ sum3); }}

From the two ends of the code above, we can see that the contents of the variable parameters and array parameters within the method are unchanged. However, the variable parameters on the call are more flexible, such as the following code:

 Public classDemo { Public int Add(int... arr) {intsum =0; for(inti =0; i < arr.length;        i++) {sum + = Arr[i]; }returnSum } Public Static void Main(string[] args) {intSUM4 =NewDemo (). Add (); System. out. println ("The result for the fourth time is:"+ sum4);intSUM5 =NewDemo (). Add (1); System. out. println ("The result for the fifth time is:"+ SUM5);intSUM6 =NewDemo (). Add (1,2,3); System. out. println ("The result for the sixth time is:"+ SUM6); }}

In the case code above, the call of SUM4 is equivalent to the call of new int[] {},SUM5 = new int[] {1}, and the SUM6 call is the equivalent of new int[] {}. However, when used in practice, mutable parameters are more flexible than arrays.

Variable parameter principle

We have now mastered the use of variable parameters. In practice, we find that mutable parameters are very similar to the form of arrays, but are more flexible when invoked. Let's look at how the variable parameter works, and then decompile the code for the variable parameter above to get the following result:

 Public classdemo{ Public int Add(int[] arr) {intsum =0; for(inti =0; i < arr.length; ++i) sum + = Arr[i];returnSum } Public Static void Main(string[] args) {intSUM4 =NewDemo (). Add (New int[0]); System. out. println ("The result for the fourth time is:"+ sum4);intSUM5 =NewDemo (). Add (New int[] {1}); System. out. println ("The result for the fifth time is:"+ SUM5);intSUM6 =NewDemo (). Add (1,2,3); System. out. println ("The result for the sixth time is:"+ SUM6); }}

By looking at the results of the deserialized code, we found that the variable parameters were compiled into the corresponding array contents through the Java 5 version of the compiler.

Variable parameter application

For variable parameters, there is a real application similar to the C language, called string formatting, such as the following code:

publicclass Demo {    publicstaticvoidmain(String[] args) {        // 字符串格式化案例        System.out.println("I am zhangwuji,I like games,I am 18 years old!");        System.out.println("I am zhouzhiruo,I like music,I am 18 years old!");    }}

The above code case can be rewritten in the following form using mutable parameters:

publicclass Demo {    publicstaticvoidmain(String[] args) {        // 字符串格式化案例        "zhangwuji";        "games";        int18;        "I am %s,I like %s,I am %d years old!";        System.out.printf(format, name, hobby, age);    }}

By looking at the underlying code, we know that the printf () method is done by receiving a mutable parameter:

... args) {    return format(format, args);}

Reprint Instructions: Please specify the author and the original link, thank you!

[Java5 new feature] variable parameter

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.