Why We Cannot Create a Generic Array in Java ?

Java does not allow creation of array with generics as elements.

In Java an array has to know the type information of its elements at runtime.

This information is used at runtime to throw ArrayStoreException if data type of an element to be inserted does not match the type of Array.

In case of Generics, the type information of a collection is erased at runtime by Type Erasure. Due to this array cannot use generics as elements.



You May Interest

What is a Livelock in Java ?

What is the Difference Between pass by reference and pass by valu ...

Why Do We Use JSP Instead of Servlet in Java ?

What is the Reason to Organize Classes and Interfaces in a Packag ...

What are the Differences Between a Checked and Unchecked Exceptio ...