When i create a generic class object , why do i have to pass array of Integer type and not int type ? If the typed parameter can be only reference type , then why does passing an int variable work but not int array ?
class GenericA<T extends Number> {
T arr[];
GenericA(T o[]) {
arr = o;
}
double average() {
double sum = 0;
for (int i = 0; i < arr.length; i++)
sum = sum + arr[i].doubleValue();
double average1 = sum / arr.length;
//System.out.println("the average is" +average);
return average1;
}
boolean sameAvg(GenericA<?> ob) {
if (average() == ob.average())
return true;
return false;
}
}
class GenericB {
public static void main(String[] args) {
Integer inum[] = {1, 2, 3, 4, 5}; //this cannot be int
Double inum1[] = {1.0, 2.0, 3.0, 4.0, 5.0}; //cannot be double
GenericA<Integer> ob = new GenericA<Integer>(inum);
GenericA<Double> ob1 = new GenericA<Double>(inum1);
boolean a = ob.sameAvg(ob1);
System.out.println(a);
}
}