Quick revision
The short answer
Covariant arrays let Java 1.0 write general methods like Arrays.sort(Object[]) before generics existed, at the cost of a run-time check on every reference store. Generics, added in Java 5, are invariant and erased, so they cannot work with arrays safely: we cannot write new T[] or new List<String>[], and mixing the two produces unchecked warnings and heap pollution. So libraries today use collections in public APIs, keep arrays as an internal detail, and bridge the two worlds with Collection.toArray(T[]) and toArray(IntFunction).
What to remember
In this answer, we saw that covariant arrays were a Java 1.0 solution for writing general methods like Arrays.sort(Object[]) without generics, paid for with run-time store checks. Generics, added in Java 5, are invariant and erased, so they cannot create arrays of a type parameter or of a parameterised type, and unchecked casts between the two worlds cause heap pollution that fails far from its cause. Collection.toArray(T[]) and toArray(IntFunction) are the bridges, and the lasting design rule is to use collections in public APIs, keep arrays internal, and never push unchecked warnings onto callers.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Covariant arrays let Java 1.0 write general methods like Arrays.sort(Object[]) before generics existed, at the cost of a run-time check on every reference store. Generics, added in Java 5, are invariant and erased, so they cannot work with arrays safely: we cannot write new T[] or new List<String>[], and mixing the two produces unchecked warnings and heap pollution. So libraries today use collections in public APIs, keep arrays as an internal detail, and bridge the two worlds with Collection.toArray(T[]) and toArray(IntFunction).
Why this question is asked
Arrays and generics are two type systems living in one language, designed fifteen years apart with opposite rules. For application code the mismatch is mostly invisible, but anyone who designs a shared library meets it quickly: a generic method cannot create the array it wants, a varargs parameter of type T produces warnings at every caller, and a cast that looks harmless fails far away. Interviewers ask this at a senior level to see whether we understand where those problems come from and how they shape API decisions. In this answer, we will recap why arrays are covariant and generics invariant, see why erasure makes generic arrays impossible, look at the bridge methods the JDK provides, and turn all of this into design rules for libraries.
1Why Java 1.0 Made Arrays Covariant
Java 1.0 had no generics. Without covariance, a method like Arrays.sort(Object[] a) could only accept arrays created exactly as Object[], and we would need a separate sort method for every element type. Covariance solved this: since String[] is a subtype of Object[], one method could sort any array of objects, comparing elements through Comparable.
The price was type safety. Because a String[] can be seen as an Object[], code could try to store any object into it. To keep arrays consistent, the JVM checks every store into a reference array at run time and throws ArrayStoreException when the type is wrong. So covariance moved a class of type errors from the compiler to the running program.
2Why Generics Are Invariant and Erased
When generics arrived in Java 5, the designers made two choices. First, generic types are invariant: List<String> is not a List<Object>. The compiler can then guarantee that a List<String> never receives an Integer, so no run-time store check is needed. Where covariance is useful, wildcards such as List<? extends Number> provide it safely, allowing reads but not writes.
Second, generics are implemented with erasure. The type arguments exist only at compile time; at run time a List<String> and a List<Integer> are both just List. This kept compatibility with millions of lines of pre-generics code and libraries, but it means generic objects do not know their own type arguments.
Arrays are the opposite on both points: covariant, and reified, meaning they know their component type at run time. That is where the two collide.
| Property | Arrays | Generics |
|---|---|---|
| Subtyping | covariant: String[] is an Object[] | invariant: List<String> is not List<Object> |
| Type at run time | reified: knows String[] | erased: just List |
| Where a wrong store is caught | at run time, ArrayStoreException | at compile time |
| Create with a type parameter | new T[] is not allowed | new ArrayList<T>() is fine |
3Why We Cannot Create Generic Arrays
To create an array, the JVM must know its component type, because the array will check every store against it. Inside a generic method, T has been erased, so the JVM cannot know what T is. That is why new T[n] is a compile error.
The unchecked cast in the second line is the usual workaround, and ArrayList does essentially this internally with an Object[]. It is safe only while the array stays inside the class. The moment the array escapes with its pretended type T[], the caller's code contains a hidden cast to String[] and fails, because the real object is an Object[]. This is called heap pollution: a variable of a parameterised type points to an object that is not of that type.
new List<String>[3] is banned for the opposite reason. If it were allowed, covariance would let us view it as Object[] and store a List<Integer> into it; the store check would pass, because at run time both are just List, and we would get a ClassCastException far away. With List<?>, which says nothing about the element type, there is no false promise to break.
4The Bridges: toArray(T[]) and toArray(IntFunction)
Because generic code cannot create a correctly typed array on its own, the collections API asks the caller to supply the type. Collection.toArray() with no argument can only return an Object[]. toArray(T[] a) takes an array whose run-time type tells the collection what to create, and since Java 11, toArray(IntFunction<T[]> generator) takes a function that creates the array.
These methods exist only to cross from the erased generic world into the reified array world. Library code that needs to do the same in its own generic methods uses the same trick: accept a Class<T> and call java.lang.reflect.Array.newInstance, or accept an IntFunction<T[]>.
5Design Rules for Libraries Today
The practical consequences are a few rules that most mature libraries follow. Public APIs take and return collections, such as List<T> or Set<T>, not arrays, because collections are type-checked at compile time, can be made unmodifiable, and work naturally with generics. Arrays stay inside the implementation, where their speed and compactness matter and the unchecked casts are contained in one class.
Generic varargs, such as <T> List<T> of(T... items), create a generic array at every call site, so the compiler warns about possible heap pollution. A library should either mark such methods @SafeVarargs, only when the method never stores or returns the array, or avoid generic varargs. Pushing unchecked warnings onto callers is a sign the API is wrong, because callers cannot fix them and learn to ignore warnings in general.
Primitive arrays are the exception: int[] or double[] have no generics problem and no boxing, so APIs for numeric data may still use them, with defensive copies or read-only wrappers to keep encapsulation.
6Follow-up questions
Why could Java not simply make generics reified like C#?
Mostly compatibility. Erasure let generic and pre-generic code work together: an old library returning a raw List could be used by new code as a List<String>, and old binaries kept running unchanged. Reified generics would have needed new collection classes or a new class file model, splitting the ecosystem.
Is (T[]) new Object[n] always wrong?
No. It is the standard internal technique, used inside ArrayList and many other collections. It is safe as long as the array is never exposed as a T[] outside the class. The warning should be suppressed on that one line with a comment explaining why it is safe.
Does Project Valhalla change any of this?
Valhalla's plans include better support for generics over primitive and value types, but those parts are not delivered in Java 25, so check the current JDK. Today's rules about erasure and generic arrays remain in force, and library designs should follow them.
7Conclusion
In this answer, we saw that covariant arrays were a Java 1.0 solution for writing general methods like Arrays.sort(Object[]) without generics, paid for with run-time store checks. Generics, added in Java 5, are invariant and erased, so they cannot create arrays of a type parameter or of a parameterised type, and unchecked casts between the two worlds cause heap pollution that fails far from its cause. Collection.toArray(T[]) and toArray(IntFunction) are the bridges, and the lasting design rule is to use collections in public APIs, keep arrays internal, and never push unchecked warnings onto callers.
Open with
Arrays and generics follow opposite rules, and that mismatch shapes library design. Arrays were made covariant in Java 1.0 because there were no generics, and covariance let one method like Arrays.sort(Object[]) work for every reference array.
Then, if they want more
The cost is that the JVM has to check every store into a reference array at run time. Generics came in Java 5 and are the other way round: invariant, so List<String> is not a List<Object>, and erased, so at run time there is just List.
Because an array needs to know its component type when it is created, and T is erased, we can't write new T[] or new List<String>[]. The usual workaround is an unchecked cast of an Object[], which is safe only while the array stays private; if it escapes as T[], the caller gets a ClassCastException somewhere unrelated. That is heap pollution.
The JDK bridges the two worlds with toArray(T[]) and, since Java 11, toArray(IntFunction), where the caller supplies the array type. So in a library I put collections in the public API, keep arrays internal for performance, use @SafeVarargs only where it is truly safe, and make sure callers never get unchecked warnings from using my API.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 5, Java 11, Java 25
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Array covariance was a Java 1.0 decision, while generics added in Java 5 are invariant....": https://scrivio-interview-library.netlify.app/java/arrays/73a662abc5/