Rookie growth Diary: Java Fundamentals 2---dynamic parameter types

Source: Internet
Author: User

Dynamic parameter types: Also known as variable parameter types or indeterminate parameter types

The dynamic parameter type mainly solves the problem of cumbersome parameter input, or the uncertainty of the number of parameters,
When the method is called, the JVM will proactively convert the parameters into arrays for processing (see example)

Issues to be aware of using dynamic parameters:
1, dynamic parameters can be the same type of array, or can be different, not the same type as object (see example)
2, the dynamic parameters in the method should be at the end of the parameter list, and only one
3, dynamic parameters can not specify the size

Nonsense don't say that directly on the code

1 /**2 * Dynamic Parameters3  * @authorWangchong4 * @date September 26, 2017 pm 3:09:105  */6  Public classDynamicparametertest {7     8     Privateobject[] messages;9   Ten     //Here you can see that the message is an array of indeterminate size One      Publicdynamicparametertest (Object ... message) { A          This. Messages =message; -          This. messagetostring (message); -     } the      -      Publicobject[] Getmessagecontent () { -         returnmessages; -     } +  -      Public intgetmessagelength () { +         returnmessages.length; A     } at      -     Private voidmessagetostring (Object ... messages) { -          for(Object message:messages) { - System.out.println (message); -         } -     } in      -      Public Static voidMain (string[] args) { to@SuppressWarnings ("Unused") +Dynamicparametertest test1 =NewDynamicparametertest (1, ' A ', "ADC"); -     }  the}

Rookie growth Diary: Java Fundamentals 2---dynamic parameter types

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.