Quick revision
The short answer
list.toArray() always returns an Object[], and the cast (T[]) inside the method is unchecked: after erasure T[] becomes Object[], so the cast does nothing and the method happily returns an Object[]. The failure appears in the caller, where the compiler inserted a cast to String[] that fails because the array really is an Object[]. The fix is to create a correctly typed array, with list.toArray(String[]::new) or by passing a Class<T> or an IntFunction<T[]>, and to treat the unchecked warning as a sign that type safety is no longer proven.
What to remember
In this answer, we saw that list.toArray() returns an Object[], and the cast to T[] inside the generic method is unchecked and erased to Object[], so nothing fails there and an Object[] escapes. The caller has a compiler-inserted cast to String[], which fails because arrays check their real type at run time. The fixes are to let the caller create the array with toArray(String[]::new) or an IntFunction<T[]>, or to pass a Class<T> and use Array.newInstance, and the lesson is that unchecked warnings mark lost type safety and should only be suppressed with a reason.
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.
list.toArray() always returns an Object[], and the cast (T[]) inside the method is unchecked: after erasure T[] becomes Object[], so the cast does nothing and the method happily returns an Object[]. The failure appears in the caller, where the compiler inserted a cast to String[] that fails because the array really is an Object[]. The fix is to create a correctly typed array, with list.toArray(String[]::new) or by passing a Class<T> or an IntFunction<T[]>, and to treat the unchecked warning as a sign that type safety is no longer proven.
Why this question is asked
This is a short piece of code with a confusing failure: the method compiles with a warning, runs without complaint, and the exception is thrown on a line that only calls it. Interviewers like it because the answer requires understanding erasure, the difference between arrays and generics, and where the compiler hides its casts. It is also a real bug that appears in utility classes. In this answer, we will run the code and read the exception, trace exactly what the erased method does, find the hidden cast in the caller, and then fix it in three ways.
1Reproducing the Failure
Here is the code from the question, with a print statement inside the method so we can see that it finishes.
Notice two things. The method prints Object[] and returns normally, so nothing failed inside it. And the stack trace points at the caller's line 7, not at the cast inside toArray. The name [Ljava.lang.Object; is how the JVM writes the class of an Object[].
2What the Method Really Does After Erasure
List.toArray() with no arguments is specified to return an Object[], because a list does not know its element type at run time; erasure removed it. So the array that comes back is an Object[] containing "a".
Now the cast. Inside the method, T is a type parameter with no bound, so after erasure T becomes Object and T[] becomes Object[]. The cast (T[]) is therefore a cast from Object[] to Object[], which is always true, and javac does not even emit a check for it. This is exactly what the warning unchecked cast means: the compiler is telling us it cannot verify this cast at run time and is trusting us.
So the method returns an Object[] while its signature promises a T[], which the caller will treat as String[].
3The Hidden Cast in the Caller
The caller wrote String[] s = toArray(List.of("a")). The compiler inferred T as String, so from the source's point of view the method returns String[]. But the erased method returns Object[], so javac inserts a checkcast to String[] at the call site, the same way it inserts casts for every generic return value.
Arrays, unlike generics, know their element type at run time. An Object[] is not a String[], even when every element happens to be a string, so the checkcast fails. That is why the exception is thrown in the caller: the compiler's inserted cast is the first place where the lie told by the unchecked cast is actually checked.
6 messages in order. Play them, or step through them one at a time.
The unchecked cast lets the wrong array escape; the compiler-inserted cast in the caller catches it.
4Fix 1: Let the Caller Pass an Array Generator
The caller is the only place that knows the real element type, so the cleanest fix is to let it supply a way to create the array. Since Java 11, Collection has toArray(IntFunction<T[]> generator), which we can pass a constructor reference to.
Notice that the unchecked warning is gone. The array is created by String[]::new, so it really is a String[], and no cast is needed anywhere.
5Fix 2: Pass a Class Token
When we need the element type for more than creating an array, a Class<T> parameter carries it. java.lang.reflect.Array.newInstance creates an array of a given component type at run time.
There is still an unchecked cast here, because Array.newInstance returns Object. But this time it is true: the array really is a T[], and the comment says why. That is the difference between a justified suppression and the one in the original code.
6What the Warning Was Telling Us
The original method had an unchecked cast warning, and it was the honest description of the bug. An unchecked warning marks a place where the compiler can no longer guarantee type safety, so any failure will appear later, somewhere else. We should never suppress it without being able to explain, in a comment, why the cast is actually correct. If we cannot write that sentence, the warning is pointing at a bug, as it was here. Often the better design, as in fix 1, avoids the cast altogether.
7Follow-up questions
Why does list.toArray(new String[0]) work?
Because the array we pass carries its run-time type. The method uses it as a template: if it is big enough it fills it, otherwise it creates a new array of the same component type with Array.newInstance. Either way the result really is a String[].
Would the failure happen if the caller wrote Object[] o = toArray(List.of("a"))?
No. Then T is inferred as Object, the inserted cast is to Object[], and it succeeds. That is what makes this bug sneaky: it depends on how each caller uses the result.
Is there any way to create a T[] without the caller's help?
Only if we already have an instance of the right array type, or a Class<T>. A generic method alone has no way to know T at run time, which is why the JDK's own APIs take a generator or an array.
8Conclusion
In this answer, we saw that list.toArray() returns an Object[], and the cast to T[] inside the generic method is unchecked and erased to Object[], so nothing fails there and an Object[] escapes. The caller has a compiler-inserted cast to String[], which fails because arrays check their real type at run time. The fixes are to let the caller create the array with toArray(String[]::new) or an IntFunction<T[]>, or to pass a Class<T> and use Array.newInstance, and the lesson is that unchecked warnings mark lost type safety and should only be suppressed with a reason.
Open with
list.toArray with no arguments always returns an Object[], because the list does not know its element type at run time. Inside the generic method, T erases to Object, so the cast to T[] is really a cast to Object[].
Then, if they want more
That is why javac says unchecked: it cannot check it, and in fact it emits no check at all. So the method returns an Object[] while promising a T[]. In the caller, T is inferred as String, so the compiler inserts a cast to String[] on the returned value.
Arrays are reified, they know their component type, and an Object[] is not a String[] even if it only holds strings, so that inserted cast throws ClassCastException on the caller's line. The bug is in the method, but the failure shows up somewhere else, which is the typical sign of an unchecked cast that was wrong.
To fix it, I let the caller supply the array type: list.toArray(String[]::new), or a method that takes an IntFunction<T[]>, which needs no cast at all. If I need the type for more, I take a Class<T> and use Array.newInstance. And I would not suppress an unchecked warning unless I can write down why it is safe.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 11
- 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 "This code compiles with a warning and throws ClassCastException at the call site, not...": https://scrivio-interview-library.netlify.app/java/generics/ada2b4d84a/