2

I'm currently studying Java Collections Framework, and one of the fascinating questions that I keep asking myself is "How do all these collections implement the generic version of Collection#toArray method". From what I've read in other SO questions, it most probably uses Array#newInstance. Just for the reference, that's how Array#newInstance is declared:

public static Object newInstance(Class<?> componentType,
                     int... dimensions)
                     throws IllegalArgumentException,
                            NegativeArraySizeException

Now, my question is: how to make the information supplied to that method enough to produce an array object of a specified type?

The multi-dimensionality aspect isn't that difficult, as one could always construct an N-dim array as a single-dim array of (N-1)-dim arrays. What really bugs me is how to create an object of type T[] from a Class<T> object passed via the Class<?> reference.

4
  • 1
    In Oracle JDK newInstance delegates to a private and native method which actually returns the new array. The actual creation is done dynamically by the JVM. Commented Mar 12, 2015 at 15:23
  • @SotiriosDelimanolis, so, this method is actually the only way to create an array whose type is supplied in run-time? No way to achieve the same using the instruments Java itself supplies? Commented Mar 12, 2015 at 15:26
  • You cannot do this - generics do not exist at runtime. If you look again at the API for Collections, you can see that the empty parameter version of toArray returns an Object[]. To get a parameterized/typed array, you must create that array first and pass it into the method. Commented Mar 12, 2015 at 15:27
  • 1
    This is an instrument that Java supplies. And, yes, this is the only way to do it (minus some Serializable, but ignore that). Commented Mar 12, 2015 at 15:28

2 Answers 2

1

If you think about it, it makes sense to have such a functionality in the Reflection API. When you do new SomeType[N], the compiler basically compiles into the byte code something that says, create a new array, with component type SomeType, and length N.

So the JVM must have some mechanism at runtime already to evaluate such instructions, that given a given component type, and length, allocates a new array. The only problem with new SomeType[N] is that the component type is hard-coded at compile-time. But it would be no more difficult for the JVM if the type were given at runtime, since the JVM has a runtime mechanism that takes the type. Since there is no native syntax that allows you to create an array with a dynamic type, the Reflection API provides a method to do it.

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

1 Comment

Thank you for your input. Although my main question was about whether it's possible to achieve the same functionality that Array.newInstance provides by not relying on any native methods. Apparently @SotiriosDelimanolis has already given an answer to that in his comments.
0

It's logically correct to have this ability and jdk offer this by Array.newInstance method. But the implementation is inside the jvm, so you cannot know how they exactly implement this in non-opensource jvm. I suggest you to examine the openJVM source code because they offer the similar ability.

Comments

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.