title: Why can't I create an array of a type parameter in Java?
body: <p>Well, I have read a lot of answers to this question, but I have a more specific one. Take the following snippet of code as an example.</p>
  
  <pre><code>public class GenericArray&lt;E&gt;{
      E[] s= new E[5];
  }
  </code></pre>
  
  <p>After type erasure, it becomes</p>
  
  <pre><code>public class GenericArray{
      Object[] s= new Object[5];
  }
  </code></pre>
  
  <p>This snippet of code seems to work well. Why does it cause a compile-time error?</p>
  
  <p>In addition, I have known from other answers that the following codes work well for the same purpose.</p>
  
  <pre><code>public class GenericArray&lt;E&gt;{
      E[] s= (E[])new Object[5];
  }
  </code></pre>
  
  <p>I've read some comments saying that the piece of code above is unsafe, but why is it unsafe? Could anyone provide me with a specific example where the above piece of code causes an error?</p>
  
  <p>In addition, the following code is wrong as well. But why? It seems to work well after erasure, too.</p>
  
  <pre><code>public class GenericArray&lt;E&gt;{
          E s= new E();
      }
  </code></pre>