Scrivio Interview library
ConceptFoundations5 min read · updated 12 November 2026

Why does List<Object> objs = new ArrayList<String>(); not compile, while Object[] arr = new String[1]; does?

Generic types are invariant: List<String> is not a subtype of List<Object>, even though String is a subtype of Object, so the assignment does not compile. If it did, objs.add(1) would put an Integer into a list of strings and break type safety. Arrays are covariant, so String[] is a subtype of Object[], and Java pays for that by checking every store at run time and throwing ArrayStoreException. To accept any list for reading, we use List<? extends Object> or simply List<?>.

Why this question is asked

This question looks like a quirk of syntax, but it explains a large part of how generics work and why wildcards exist. Every Java developer eventually writes a method that takes List<Object>, tries to pass a List<String>, and gets a compile error that seems unreasonable. Interviewers ask it to see whether we can explain the rule from first principles, using the bug it prevents, and contrast it with arrays, which made the opposite choice. In this answer, we will recap subtyping, see why generics must be invariant, show how arrays fail at run time instead, and finish with the wildcard that lets us accept any list.

1A Quick Recap of Subtyping

Type A is a subtype of type B when a value of A can be used anywhere a B is expected. String is a subtype of Object, so Object o = "hi" compiles. The question is whether that relationship carries over to containers: is a list of strings a kind of list of objects?

There are three possible answers, with names. Covariant means yes, the container follows its element type: String[] is a subtype of Object[]. Invariant means no relationship at all: List<String> and List<Object> are unrelated types. Contravariant means the reverse direction, which Java expresses with ? super, as we will see in other questions. Java's arrays are covariant and Java's generic types are invariant.

2Why Generic Types Must Be Invariant

Suppose the assignment were allowed. Here is what would follow.

Java
List<String> strings = new ArrayList<>();
List<Object> objs = strings; // error: incompatible types: List<String> cannot be converted to List<Object>
// If it had compiled:
objs.add(1); // allowed: 1 is an Object
String s = strings.get(0); // ClassCastException: Integer cannot be cast to String
Step throughWhat would go wrong if List<String> were a List<Object>
Callerobjs (List<Object>)The ArrayListstrings (List<String>)
objs.add(1)
store Integer 1; no run-time check
strings.get(0)
read element 0
ClassCastException far from the bug

5 messages in order. Play them, or step through them one at a time.

Both references point to one list, and erasure means the list cannot defend itself.

Notice that objs and strings would be two names for the same list. Through objs, adding an Integer is perfectly legal, because an Integer is an Object. Through strings, the code believes every element is a String. The failure would appear far away from the mistake, at a get() that looks completely safe.

Generics are checked at compile time, and at run time the element type is erased, so the list itself has no idea it is meant to hold strings. That means Java cannot check the add at run time the way it can for arrays. The only safe option is to reject the assignment at compile time, which is exactly what invariance does.

3Arrays Made the Opposite Choice

Arrays existed long before generics, and Java made them covariant. That is why the second line compiles.

Java
Object[] arr = new String[1]; // compiles: String[] is a subtype of Object[]
arr[0] = "ok"; // fine
arr[0] = 1; // throws java.lang.ArrayStoreException: java.lang.Integer
Arrays versus generic lists
QuestionArraysGeneric lists
Is String[] / List<String> a subtype of Object[] / List<Object>?yes: covariantno: invariant
When is a wrong store caught?at run timeat compile time
What is thrown?ArrayStoreExceptionnothing: it never compiles
Does the container know its element type at run time?yesno: erased

The same bug is possible here, but Java catches it at run time. Unlike a generic list, an array knows its real element type at run time: this array was created as a String[], and the JVM checks every store into it. Storing an Integer fails immediately with ArrayStoreException, at the line that caused it.

So arrays trade compile-time safety for flexibility and pay with a check on every store. Covariant arrays were useful before generics, for example so that a method like Arrays.sort(Object[]) could sort any array of objects. With generics, Java chose to catch the mistake at compile time instead, which is safer and costs nothing at run time.

4Accepting Any List for Reading

Invariance would be very limiting if it were the whole story. A method that prints any list should accept List<String>, List<Integer> and List<Object>. That is what wildcards are for.

Java
static void printAll(List<?> items) { // same as List<? extends Object>
for (Object o : items) System.out.println(o);
}
printAll(List.of("a", "b")); // a, b
printAll(List.of(1, 2)); // 1, 2
static void broken(List<?> items) {
items.add("x"); // error: the compiler does not know what the list holds
}

List<?> means a list of some type we do not know. Every element is at least an Object, so reading is safe. Adding is not, because the list might be a List<Integer>, so the compiler refuses any add except null. This is the same protection invariance gives, but now applied only to the operation that is unsafe, while reading stays available. List<? extends Object> means the same thing; List<?> is just the shorter spelling.

5Follow-up questions

Why does Java not check generic adds at run time like arrays?

Because of erasure. Generics were added in Java 5 in a way that kept old class files and libraries working, so the type argument is removed at compile time and an ArrayList at run time is just an ArrayList. There is nothing to check against.

Can we create a generic array such as new T[10] or new List<String>[10]?

No. Since arrays check stores using their run-time element type, and generic types are erased, the array could not perform a correct check. Java forbids creating arrays of non-reifiable types to avoid silently unsafe arrays. We use List<List<String>> instead.

Is List<Object> ever the right parameter type?

Yes, when the method needs to add arbitrary objects to the list the caller gives it. Then only a real List<Object> is safe, and the invariance is doing its job. For reading only, List<?> is the better choice.

6Conclusion

In this answer, we saw that generic types are invariant, so List<String> is not a List<Object>. If it were, adding an Integer through the List<Object> reference would corrupt the list, and erasure means the list could not detect it. Arrays are covariant and protect themselves at run time with ArrayStoreException, because they know their element type. When we want to accept any list for reading, we use List<?> or List<? extends Object>, which allows reads and rejects adds.

What to say in the interviewAbout 80 seconds aloud

Open with

Generic types in Java are invariant, so List<String> is not a subtype of List<Object>, even though String is a subtype of Object. The reason is type safety. If the assignment compiled, objs and the original list would be the same object, and through objs I could legally call add(1), because an Integer is an Object.

Then, if they want more

Then code reading the list as strings would get a ClassCastException far away from the real mistake. Because of erasure, the ArrayList does not know at run time that it should hold strings, so Java cannot check the add at run time; the only safe place to stop it is at compile time, and invariance does that.

Arrays made the opposite choice. They are covariant, so Object[] arr = new String[1] compiles, but an array knows its real element type at run time, so storing an Integer into it throws ArrayStoreException immediately. So arrays catch the error at run time with a check on every store, and generics catch it at compile time.

If I want a method that accepts any list just for reading, I use List<?> or List<? extends Object>. That lets me read elements as Object, while the compiler refuses adds other than null, which keeps the same safety.

About this answer

Last updated
12 November 2026
Versions it names
Java 5
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.
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 "Why does List<Object> objs = new ArrayList<String>(); not compile, while Object[] arr =...": https://scrivio-interview-library.netlify.app/java/generics/1700f6d6e4/