I've heard here and there that arrays contain runtime data about their type, making you unable to instantiate an E[] (E being a generic type parameter, for example in a class Foo<E>), and that while you can achieve the same effect by doing (E[]) new Object[n], it is bad, which is why the compiler raises a warning. I'm having trouble thinking of what exactly could go wrong (famous last words), so could someone give an example of how a program could become defective because of instantiating Object[]s and casting them to E[]?
Example of problem caused by casting Object[] to E[]
java;array;generics;defect;warnings
If the array is only used within the inside of the class, and the fact of the variable's type as E[] is never exposed outside the class (e.g. returned in a method, or it is a public or protected field, etc.), then there is no problem as inside the class E is erased.However, if you expose the fact of the array variable's type as E[] to the outside of the class, then it can go into a context where they have a concrete type for your E, and rightly believe that what they are getting is that type of array, and it will throw a ClassCastException without any warning in that outside code. The simplest example is a method that simply returns the array as E[], and it is called from outside code that has a concrete type as the type parameter; a ClassCastException is thrown from the outside code without any warning in that outside code or in the method that returns the array:public class Foo<E> {    private E[] myArray = (E[])new Object[42];    public E[] getArray() {        return myArray;    }}// some outside code:Foo<String> foo = new Foo<String>();String[] stringArray = foo.getArray(); // ClassCastException