I've got a question referring to this post: Java generics to enforce return type of abstract method and Björn's answer in particular: https://stackoverflow.com/a/1627646
Isn't it possible to use primitive data types for the data type specifier? Specifically, I have the problem that I have an abstract class "Matrix" and two sub-classes which inherit from "Matrix". One is "ComplexMatrix" which basically is a Matrix with complex entries. Therefore, I created a class "Complex". The other is a "normal" double matrix.
public abstract class Matrix<T> {
public abstract T getValue(int r, int c);
}
public class ComplexMatrix extends Matrix<Complex> {
public Complex getValue(int r, int c){
return null;
}
}
public class DoubleMatrix extends Matrix<double> {
public double getValue(int r, int c){
return double.NaN;
}
}
The first one, ComplexMatrix, works. The second one does't. It says for < double>: "unexpected type required reference found double".
If I use the Wrapper class Double
public class DoubleMatrix extends Matrix<Double> {
public double getValue(int r, int c){
return Double.NaN;
}
}
the error message says, that "return type double is not compatible with Double" where T is a type variable.
Can someone please tell me why this isn't working? Why does it work with "< String>"?
Thanks.