3

I am unable to understand why this code is not compiling...

public class Room {
    public static void main(String[] args) {
        Double[] ar = {1.1,2.23,3.56,4.23,5.90,6.88,7.99,8.09,9.88,10.99};
        Average<Double> tt = new Average<>(ar);
        tt.summ();
    }
}

class Average<T extends Double> {
    T[] arr;
    T sum;
    Average(T[] num) {
        arr = num;
    }

    void summ() {
        for (T i : arr) {
            sum = sum + i;//Shows incompatible types
            System.out.println(sum);
        }
    }
}

The compiler error states:

Error on Room.java, line 18:

Type mismatch: cannot convert from double to T

Can somebody please explain why this code is not compiling??

Community
  • 1
  • 1
Frosted Cupcake
  • 1,909
  • 2
  • 20
  • 42

4 Answers4

5

Eran is right; java.lang.Double is final, so it makes no sense to have a type parameter T extends Double. The only possible type that satiesfies this is Double itself, so you can just as well remove the type parameter and just use Double directly (or better yet: the primitive type double).

The reason why your code doesn't compile is because you are trying to use the + operator on objects of type T.

The Java compiler is not so smart that it notices that T can only be Double, so that it automatically can convert (by auto-unboxing and -boxing) the value to a double to do the calculation.

Maybe you come from a C++ background and you've used C++ templates. Java generics do not work in the same way as C++ templates; generic types in Java are not templates from which code is generated.

Jesper
  • 202,709
  • 46
  • 318
  • 350
  • "*so that it automatically can convert (by auto-unboxing and -boxing) the value to a double to do the calculation.*" - **this** is the actual answer to the actual question – Andy Brown Jul 04 '15 at 12:46
3

There is no point in this generic type parameter having this bound :

class Average<T extends Double>

since Double is final and can have no sub-types.

Therefore you might as well remove the generic type parameter T and replace T with Double everywhere.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • In this case, OP might want to substitute `T` with `double` instead of `Double`, which gets rid of the compilation error. – Turing85 Jul 04 '15 at 12:36
  • 1
    @Eran,but why the code is not compiling?..Even if I write,then also its showing compilation error – Frosted Cupcake Jul 04 '15 at 12:37
  • @Codebender The OP's code gives a compilation error - `Type mismatch: cannot convert from double to T` on the line `sum=sum+i;` – Eran Jul 04 '15 at 12:37
  • if we could teach java compiler which type to use during compilation then it could work – Roman C Jul 04 '15 at 12:37
  • 3
    @RajMalhotra read the "possible duplicate of..." comment given by Andy Brown. This should answer your question. – Turing85 Jul 04 '15 at 12:39
0

Since I don't know what exactly your code should archive, I am not sure about the correctness of this answer, but a quick fix (at least to avoid syntax error) is to substitute the line:

T sum;

with:

double sum;

With this, your output is the incremental sum of all the double, one by one.

Ruben Rizzi
  • 342
  • 1
  • 3
  • 20
0

+ is arithmetic operator to use with numbers or strings, but it cant be used with Object type. If you want to use + operator both operand should have the same type.

class Average<T extends Double> {
  T[] arr;
  double sum = 0;
  Average(T[] num) {
    arr = num;
  }

  void summ() {
    for (T i : arr) {
      sum += (Double)i;//Shows incompatible types
      System.out.println(sum);
    }
  }
}
Roman C
  • 49,761
  • 33
  • 66
  • 176