[New Features of Java 5] variable parameters

Source: Internet
Author: User

[New Features of Java 5] variable parameters
What are variable parameters?

In the basic content of Java, functions have a feature: overload. If you want to add multiple numbers, follow the code below:

Public class Demo {public int add (int a, int B) {return a + B;} public int add (int a, int B, int c) {return a + B + c;} public static void main (String [] args) {int sum1 = new Demo (). add (1, 2); int sum2 = new Demo (). add (1, 2, 3); System. out. println (first result: + sum1 +; second result: + sum2 );}}

However, it is not concise to set multiple corresponding methods. We can simplify it by using arrays.

Public class Demo {public int add (int [] arr) {int sum = 0; for (int I = 0; I <arr. length; I ++) {sum + = arr [I];} return sum;} public static void main (String [] args) {int sum3 = new Demo (). add (new int [] {1, 2, 3, 4, 5}); System. out. println (the third result is + sum3 );}}

After Java 5, variable parameters are provided, as shown in the following code:

Public class Demo {public int add (int... arr) {int sum = 0; for (int I = 0; I <arr. length; I ++) {sum + = arr [I];} return sum;} public static void main (String [] args) {int sum3 = new Demo (). add (new int [] {1, 2, 3, 4, 5}); System. out. println (the third result is + sum3 );}}

From the above code, we can see that the content of variable parameters and array parameters in the method has not changed. However, variable parameters must be more flexible in calling, such as the following code:

Public class Demo {public int add (int... arr) {int sum = 0; for (int I = 0; I <arr. length; I ++) {sum + = arr [I];} return sum;} public static void main (String [] args) {int sum4 = new Demo (). add (); System. out. println (the fourth result is + sum4); int sum5 = new Demo (). add (1); System. out. println (the fifth result is + sum5); int sum6 = new Demo (). add (1, 2, 3); System. out. println (the sixth result is + sum6 );}}

In the above case code, the call of sum4 is equivalent to new int [] {}, and the call of sum5 is equivalent to new int [] {1 }, the call to sum6 is equivalent to new int [] {, 3 }. In actual use, variable parameters are more flexible than arrays.

Variable Parameter Principle

Currently, we have mastered the usage of variable parameters. In actual use, we found that variable parameters are similar to arrays, but they are more flexible during calls. Next we will study the principle of variable parameters. After decompiling the code of the variable parameters above, we will get the following results:

Public class Demo {public int add (int [] arr) {int sum = 0; for (int I = 0; I <arr. length; ++ I) sum + = arr [I]; return sum;} public static void main (String [] args) {int sum4 = new Demo (). add (new int [0]); System. out. println (the fourth result is + sum4); int sum5 = new Demo (). add (new int [] {1}); System. out. println (the fifth result is + sum5); int sum6 = new Demo (). add (1, 2, 3); System. out. println (the sixth result is + sum6 );}}

After viewing the decompiled Code results, we found that the variable parameters are compiled into the corresponding array content through the Java 5 compiler for transmission.

Variable Parameter Application

Corresponding to variable parameters, there is a practical application similar to the C language, called string formatting, for example, the following code:

Public class Demo {public static void main (String [] args) {// String formatting case 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 !); }}

In the above Code case, you can use variable parameters to rewrite them to the following form:

Public class Demo {public static void main (String [] args) {// String formatting case String name = zhangwuji; String holobby = games; int age = 18; string format = I am % s, I like % s, I am % d years old !; System. out. printf (format, name, holobby, age );}}

By viewing the underlying code, we can know that the printf () method is completed by receiving a variable parameter:

public PrintStream printf(String format, Object ... args) {    return format(format, args);}
 

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.