title: Java Generic ： The operator - is undefined for the argument type(s) T, T
body: <p>I would like to get the difference of two images, of which the pixels are represented by two arrays of Integer, Float or so. I writed a generic method to do this:</p>
  
  <pre><code>    private &lt;T extends Number&gt; T[] pixelsDiff(T[] pxs1, 
      T[] pxs2, Class&lt;T&gt; clazz) {
  
          if (pxs1.length != pxs2.length)
              return null;
  
          T[] diff = (T[]) Array.newInstance(clazz, pxs1.length);
  
          for (int i = 0; i &lt; pxs1.length; i++) 
              diff[i] = (pxs1[i] - pxs2[i]);
  
          return diff;
  
  }
  </code></pre>
  
  <p>whihle there was a error on </p>
  
  <pre><code>diff[i] = (pxs1[i] - pxs2[i]);
  </code></pre>
  
  <p>reads</p>
  
  <pre><code>The operator - is undefined for the argument type(s) T, T
  </code></pre>
  
  <p>if I changed this line to </p>
  
  <pre><code>diff[i] = (T) (pxs1[i].doubleValue() - pxs2[i].doubleValue());
  </code></pre>
  
  <p>there would be another error : Cannot cast from double to T</p>
  
  <p>Since I can't override the "-" operator for T like c++ does, I wonder is there any way that can return a T[] array instead of a double[]?</p>