incompatible bounds compilation error
public class Complex<T extends BigDecimal, R extends BigDecimal> {
private T r;
private R i;
public Complex(T r, R i) {
this.r = r;
this.i = i;
}
@Override
public String toString() {
return "Complex{" + "r=" + r + ", i=" + i + '}';
}
public T getR() {
return r;
}
public R getI() {
return i;
}
public Complex<T,R> add (Complex<T,R> other){
return new Complex<T,R>((T) (r.add(other.r)), (R) (i.add(other.i)));
}
}
And then when calling the class constructor:
Complex<BigDecimal,BigDecimal> s = new Complex<>(12121,12121);
This compilation error occurs:
cannot infer type arguments for Complex<>
reason: inference variable T has incompatible bounds
upper bounds: BigDecimal
lower bounds: Integer
where T is a type-variable:
T extends BigDecimal declared in class Complex
Java won't convert an int
to a BigDecimal
implicitly, you need to explicitly provide a BigDecimal
type to the constructor arguments.
Complex<BigDecimal,BigDecimal> s = new Complex<>(
BigDecimal.valueOf(12121),
BigDecimal.valueOf(12121)
);