roland luo
roland luo

Reputation: 1631

What code and how does java.lang.reflect.Array create a new array at runtime?

I looked into Java source code and the method is like follows:

public static Object newInstance(Class<?> componentType, int length)
    throws NegativeArraySizeException {
    return newArray(componentType, length);
}

private static native Object newArray(Class componentType, int length)
    throws NegativeArraySizeException;

It seems it does not have any code in the method newArray() to build an array. Can anyone explain how it builds an array? T

Upvotes: 4

Views: 448

Answers (2)

rec
rec

Reputation: 10905

It is hard-coded into the JVM (not the compiler). You could download the source code of OpenJDK or any other open-source Java Virtual Machine and look there ;)

Upvotes: 1

SLaks
SLaks

Reputation: 887867

This is a native method.

That means it's implemented by native code within the JRE.

Upvotes: 5

Related Questions