title: Array instantiation of unknown type in class
body: <p>I'm trying to make my own <code>Matrix</code> class in Java.
  What I want is to specify the type (<code>float</code>, <code>long</code>, <code>int</code> ...) and size <code>M*N</code> of the <code>Matrix</code> when creating it.</p>
  <p>In order to do that, I created the class the following way: <code>public class Matrix&lt;T extends Number&gt; { ...</code></p>
  <p>The problem is that when creating the Array <code>this.m = new T[M][N];</code>, an error occurs saying <code>type parameter 'T' cannot be instantiated directly</code>, I'm guessing because different types take different sizes in the memory.</p>
  <p>The only way I could get around this was to do: <code>this.m = (T[][]) new Number[M][N];</code>
  But I have no idea if this is a good idea, or if there is a better way to do it.</p>
  <p><strong>Here is the complete code:</strong></p>
  <pre><code>public class Matrix&lt;T extends Number&gt; {
  
      /**
       * M represents the Matrix' number of rows.
       * N represents the Matrix' number of columns.
       */
      final private int M, N;
  
      /**
       * Matrix value.
       */
      private T[][] m;
  
      /**
       * Create a new Matrix instance.
       *
       * @param M Number of rows
       * @param N Number of columns
       */
      public Matrix(final int M, final int N) {
          this.M = M;
          this.N = N;
  
          this.m = (T[][]) new Number[M][N];
      }
  
  }
  </code></pre>
  <p>Thank you for any help or clarification on this.</p>