variable parameter
After the variable parameter is the JDK1.5, out of the characteristic, if we need to define a method of receiving a plurality of parameters, and a plurality of same type of parameters, we can use the variable parameters: public void test(int ... num)
the middle ...
represents the definition of a variable parameter.
Define a variable parameter and use
public static void main(String[] args) {
add(1,2,3,4,5);
}
public static void add(int... ints) {
System.out.println(ints.toString());
}
The principle of variable parameters
After the above code has been run, the compiled code .class
is:
public static void main(String[] args){
add(new int[] { 1, 2, 3, 4, 5 });
}
public static void add(int[] ints) {
System.out.println(ints.toString());
}
So, the principle of variable parameters is an array. Only when calling a method with variable parameters, you do n’t need to create an array, but when you compile, you still pack these elements into an array and then pass it. And when receiving, it is also an array to receive.
Considerations for variable parameters
- There can be only one variable parameter in a parameter list.
- If there are variable parameters in the parameter list, the variable parameters are only listed at the end.
The details determine the success or failure!
Personally, I am humbled.