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