3

In Java we have toArray() method which will return Object[]. If I want to return another data type I need to pass it to parameters like this

SomeClass.SomeList.toArray(new AnotherClass[0]);

What does 0 mean in this case?

6
  • 3
    It creates a zero-length array, i.e. []. Since arrays are fixed-size, this is an array that contains nothing (and never can contain anything). This is a common sentinel value in calling #toArray in which the method will make its own array to return Commented Aug 4 at 15:08
  • 7
    This is an old workaround for providing the array type to the toArray method if you don't know the exact length. toArray will then create a new array with the correct length and type. Since Java 11 this is no longer necessary as you can simply use someList.toArray(AnotherClass[]::new); Commented Aug 4 at 16:27
  • Related: Using 0 size array as parameter of List.toArray<T[]> Commented Aug 4 at 18:27
  • 1
    @DuncG Thanks for pointing that out. This is the behavior of java.util.Collection and it is documented: The default implementation calls the generator function with zero and then passes the resulting array to toArray(T[]). docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/…. What a nonsense and waste of CPU cycles... Commented Aug 5 at 10:54
  • 1
    @Robert there’s no waste of CPU cycles, as passing a zero size array to common implementations like ArrayList has been proven the most efficient method since Java 6. Performance in managed environments with JIT and Hotspot optimizer can heavily contradict intuition. Besides that, it’s the only way for a default method to provide the necessary implementation guarantees for arbitrary implementations, like concurrent collections, for example. Passing a sized array would break for concurrent collections and be less efficient at the same time. Commented Aug 5 at 14:35

1 Answer 1

4

new AnotherClass[0] will create an array with 0 elements.

SomeList.toArray(...) will first check if the array is big enough to fit the elements and will create a new array of the desired size if it is not large enough.

You might prefer to do someList.toArray(new AnotherClass[someList.size()])

This way you don't create a zero element array and then throw it away.

But as @Rogue suggested, this does not perform better than new AnotherClass[0] (see https://shipilev.net/blog/2016/arrays-wisdom-ancients)

Sign up to request clarification or add additional context in comments.

6 Comments

JVM and JIT have some fun optimizations that actually make the latter not as efficient: stackoverflow.com/questions/174093/…
That was an interesting rabbit hole to go down
Last comment isn't useful as stream() would end up creating objects which are thrown away.
changed to .toArray(new AnotherClass[size])
Bear in mind that evaluating size() might not be trivial for all types of List. I used to evaluate size everywhere but have switched my toArray conversions to use new T[0]
Stay with new AnotherClass[0]. It’s robust and more efficient. If you want the full story, read shipilev.net/blog/2016/arrays-wisdom-ancients

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.