At some point, we all have used variable arguments using Java. Sometimes we find it very useful but to be frank, it is not as friendly as you think. It might incur several hassles during application development.
Generic Array Creation
While writing programs, we, developers are inclined to utilize such helper methods (asList(E…)) quite often. Basically the arguments as passed to the asList(E…) are stored in an array.
Now let’s look at the equivalent snippet.
Generic Array Creation - Equivalence
Will this program compile?
No, it won’t. If you have gone through my posts on Array Reification, then you might remember that Generic Array Creation on non-refiable component type is not possible.
So how do you make the snippet work?
It’s simple, right? Just use raw type of List while array creation. But still, you would encounter the well-known Unchecked Cast Warning message.
Let’s try to change the program a bit to see some interesting facts.
What about this?
This will still report a compilation error. Due to the generic type check before the print statement, it will report the following error.
Now, we have written an equivalence snippet in which we used raw List array. Due to this, we can see that the List array would accept the list of doubles. We can clearly see that the aforementioned snippet bypassed the generic type check.
In general, the use of varargs would impose more danger than the comforts it can offer. It’s always preferable to use ArrayList in lieu of varargs.