Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

Why does this compile and then fail at run time?

Java
Object[] objs = new String[2];
objs[0] = Integer.valueOf(1);

It compiles because Java arrays are covariant: a String[] is a subtype of Object[], so the compiler accepts the assignment and then sees only an Object[] that can hold any object. It fails at run time because the array object still knows it is a String[], and the JVM checks every store into a reference array, so storing an Integer throws ArrayStoreException. With generics the same mistake does not compile, because List<String> is not a List<Object>.

Why this question is asked

This two-line puzzle shows one of the few places where Java's type system lets a type error slip past the compiler and only catches it while the program runs. Interviewers ask it because the explanation touches three ideas we use all the time: subtyping, the difference between the static type of a variable and the run-time type of an object, and why generics were designed differently from arrays. In this answer, we will recap what covariance means, follow the code line by line, see the check the JVM performs, compare it with generics, and look at the small cost of that check.

1A Quick Recap: Static Type and Run-Time Type

Every variable has a static type, the type the compiler sees in the declaration. Every object has a run-time type, the class it was really created as. In Object[] objs = new String[2], the variable objs has static type Object[], while the array object it points to has run-time type String[].

The compiler checks code using static types only. The JVM, while the program runs, knows the run-time type of every object. Most of the time the two agree closely enough that the compiler's checks are all we need. This puzzle is about a case where they do not.

2Why the Assignment Compiles

Java arrays are covariant. That means if String is a subtype of Object, then String[] is a subtype of Object[]. So a String[] can be assigned to an Object[] variable, just as a String can be assigned to an Object variable.

Java
Object[] objs = new String[2]; // compiles: String[] is a subtype of Object[]
objs[0] = "hello"; // compiles and works: a String in a String[]
objs[1] = Integer.valueOf(1); // compiles: an Integer is an Object
// run time: java.lang.ArrayStoreException: java.lang.Integer

For the last line, the compiler checks only that the value, an Integer, can be stored in an element of static type Object. It can, so the line compiles. The compiler has no way to know that this particular Object[] variable happens to point to a String[].

3Why It Fails at Run Time

Unlike generic collections, Java arrays remember their element type at run time. We say arrays are reified: the String[] object carries the information that its component type is String. Because covariance would otherwise let us put any object into any array, the JVM checks every store into an array of references. Before it writes the value, it checks that the value's class is compatible with the array's component type. An Integer is not a String, so the store is refused with ArrayStoreException, and the message names the class we tried to store.

Step throughWhat the JVM does on objs[1] = Integer.valueOf(1)
  1. Load the array referencethe object is really a String[]
  2. Check the index1 is within length 2
  3. Is Integer assignable to String?
    YesStore the valueNoThrow ArrayStoreException

3 steps in order. Play them, or step through them one at a time.

The bounds check and the type check both happen at run time on every reference store.

Notice what this check protects. If the store were allowed, the String[] would hold an Integer, and some other code reading it as a String[] would later get a ClassCastException, or worse, treat an Integer as a String. The ArrayStoreException catches the problem at the moment of the bad write, which is the best place to catch it at run time.

4How Generics Catch the Same Bug at Compile Time

Generics, added in Java 5, were designed to be invariant: List<String> is not a subtype of List<Object>, even though String is a subtype of Object. So the same mistake with lists does not compile.

Java
List<Object> objs = new ArrayList<String>(); // does not compile: incompatible types
List<? extends Object> view = new ArrayList<String>(); // allowed: a read-only view
view.add(Integer.valueOf(1)); // does not compile
Object first = view.isEmpty() ? null : view.get(0); // reading is fine

If we want a method that accepts a list of any element type for reading, we use a wildcard, List<? extends Object> or simply List<?>. The compiler then allows reads but forbids adds, which is exactly the safe part of covariance. This is the reason generics needed no run-time store check: the compiler prevents the unsafe writes. It also had to be this way, because generic types are erased at run time, so a List object does not even know that it is a List<String> and could not check a store if it wanted to.

5The Cost of the Store Check

The store check runs on every write to an array of references, such as Object[] or String[]. It does not apply to primitive arrays like int[], because an int slot can only hold an int. The check is cheap, often just a comparison of class pointers, and the JIT removes it entirely when it can prove the types match, for example when it knows the array is exactly an Object[]. So in normal code we never notice it, but it is one reason why tight loops over primitive arrays can be slightly faster than over reference arrays.

6Follow-up questions

How would you fix the code in a real project?

Declare the array with the type we really mean, String[] names = new String[2], so the compiler rejects the Integer, or use a List<String>, which gives compile-time checks without any covariance. If the array really should hold mixed values, create it as new Object[2].

Is ArrayStoreException a checked exception?

No. It extends RuntimeException, so the compiler does not force us to handle it. That fits its purpose: it signals a programming error, not a condition a caller should recover from.

Can System.arraycopy throw ArrayStoreException too?

Yes. If we copy from an Object[] that contains an Integer into a String[], arraycopy checks each element and throws ArrayStoreException when it reaches the Integer. It also throws it immediately if the two arrays have incompatible types, such as int[] and String[].

7Conclusion

In this answer, we saw that Object[] objs = new String[2] compiles because arrays are covariant, so String[] is a subtype of Object[], and the compiler then judges every store by the static type Object[]. At run time, the array still knows it is a String[], and the JVM checks every reference store, so storing an Integer throws ArrayStoreException. Generics are invariant, so List<Object> objs = new ArrayList<String>() is rejected at compile time, and wildcards give the safe read-only form of covariance. The run-time store check has a small cost on reference array writes, which the JIT often removes.

What to say in the interviewAbout 80 seconds aloud

Open with

It compiles because Java arrays are covariant: if String is a subtype of Object, then String[] is a subtype of Object[], so assigning a String[] to an Object[] variable is legal.

Then, if they want more

From then on, the compiler only knows the static type Object[], and storing an Integer into an element of type Object is fine as far as it can tell. But arrays are reified, they keep their element type at run time, and the actual object is still a String[].

To keep covariance from corrupting arrays, the JVM checks every store into a reference array, so when we write an Integer into it, it throws ArrayStoreException with the message java.lang.Integer. That check costs a little on every reference array write, though the JIT often removes it when it can prove the types.

Generics were made invariant on purpose: List<String> is not a List<Object>, so the equivalent list code fails to compile, and if we want a read-only view of any list, we use a wildcard like List<? extends Object>. That's also why I prefer collections over arrays in APIs: the mistake is caught by the compiler instead of in production.

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 this compile and then fail at run time?": https://scrivio-interview-library.netlify.app/java/arrays/06ab97345b/