title: Instantiate a generic Number class in Java
body: <p>I am trying to create an Array-like object that can store <code>Number</code> types.
  I want to initialize each value in <code>Object[] array</code> to <code>0</code>.
  If I knew that the <code>NegativeArray</code> was storing <code>Integer</code>s,
  then I could do <code>new Integer(0)</code>.
  Unfortunately, when dealing with the generic type <code>E</code>,
  I don't know how to create a new <code>Number</code> with value <code>0</code>.</p>
  
  <p>This is my current implementation:</p>
  
  <pre><code>package com.gly.sfs.util;
  
  public class NegativeArray &lt;E extends Number&gt; {
  
      private int firstIndex;
      private int lastIndex;
      private Object[] array;
  
      public NegativeArray(int firstIndex, int lastIndex) {
          this.firstIndex = firstIndex;
          this.lastIndex = lastIndex;
  
          boolean isValid = lastIndex &gt; firstIndex; 
          if (!isValid) {
              throw new IllegalArgumentException(
                      "lastIndex &gt; firstIndex violated!");
          }
  
          array = new Object[size()];
      }
  
      public void set(int index, E e) {
          checkIndexValidity(index);
          array[getIndex(index)] = e;
      }
  
      public E get(int index) {
          checkIndexValidity(index);
          @SuppressWarnings("unchecked")
          E e = (E) array[getIndex(index)];
          return e;
      }
  
      public int size() {
          return lastIndex - firstIndex;
      }
  
      private int getIndex(int index) {
          return index - firstIndex;
      }
  
      private void checkIndexValidity(int index) {
          boolean isValid = (index &gt;= firstIndex) &amp; 
                  (index &lt; lastIndex);
          if (!isValid) {
              throw new ArrayIndexOutOfBoundsException();
          }
      }
  }
  </code></pre>