.toArray(new MyClass[0]) or .toArray(new MyClass[myList.size()])?

Assuming I have an ArrayList

ArrayList<MyClass> myList;

And I want to call toArray, is there a performance reason to use

MyClass[] arr = myList.toArray(new MyClass[myList.size()]);

over

MyClass[] arr = myList.toArray(new MyClass[0]);

?

I prefer the second style, since it’s less verbose, and I assumed that the compiler will make sure the empty array doesn’t really get created, but I’ve been wondering if that’s true.

Of course, in 99% of the cases it doesn’t make a difference one way or the other, but I’d like to keep a consistent style between my normal code and my optimized inner loops…

9 Answers
9

Leave a Comment