Using Generics in Matrix Addition/Multiplication
<p>I'm trying to create a custom class the creates matrices and, among other things, performs operations that add up all of the cells or multiply them all. However, I want to use generics so the matrices can be any type of number: float, double, int, etc. I have thus set up the class like this:</p>

<pre><code>public class Matrix&lt;num&gt;
</code></pre>

<p>Upon initialization, the instantiation of this class creates a matrix based on user supplied data, stored in the instance's .matrix variable. Now, in the code where I want to add up all of the cells, I do something like this: </p>

<pre><code>public num addMatrices(num[][] toAdd){
        num result;
        if (toAdd.length != this.rows &amp;&amp; toAdd[0].length != this.columns){
            System.out.println("Matrix mismatch. Try Again.");
            return toAdd[0][0];
        }
        for (int i=0; i&lt;rows; i++)
            for (int j=0; j&lt;rows; j++){
                result = this.matrix[i][j] + toAdd[i][j];
            }
    }
</code></pre>

<p>I'm running into multiple problems, however. First of all, I can't initialize result to zero, which makes it hard to perform += operations. Secondly, when I try to add the cells of the two matrices, the compiler tells me that the + operator is undefined for the type num. </p>

<p>I thought the whole point of generics was to have a catchall type so I could do things like use floats in one case and ints in another, but if I need to specify the type for operators like +, I'm not sure where the advantage comes in... </p>
