What is the performance of methods using … parameters as opposed to methods using a single element or an array as parameters? In other words, how does code A compare to code B, performance-wise.
Code A:
public void add(final E... aObjectToAdd) {
//code here
} //end add
Code B:
public void add(final E object) {
//code here
} //end add
public void addAll(final E[] aObjectToAdd) {
//code here
} //end addAll
It is my assumption that the … method creates an array for the method even if you give it only one argument. If such is the case, then I would assume that the … method will always perform the same as the array method and worse than the single object method.
I’ve been using the … because it’s easier. However, I might not want to use it in the utility classes I’m writing if it harms performance.
If I define the following 2 methods:
public void add(final E object) {
//code here
} //end add
public void add(final E... aObjectToAdd) {
//code here
} //end add
Will it use the single object method for single objects and the other method for lists of objects? If such is the case, I will probably do it that way.