Java generics – insert internal type parameters
I'm new to Java I just want to pass comparable < string > into the method parameter of generic type < e extends comparable < E > > I believe that the meaning of < e extends comparable < E > > is any object that extends comparable Please tell me how to pass comparable < string > or any object that extends comparable < string > and there is another object
The compiler gave me the wrong inference that type comparison < string > is not a valid alternative to bounded parameters < e extends comparable < E > >
Code:
public class Compare<T> implements Comparable<T>{
public int compareTo(T o) {
return 0; // Not worried about logic
}
}
class CompareTest{
public <E extends Comparable<E>>void testGeneric(E e){
System.out.println("Executed");
}
public static void main(String args[]){
Compare<String> compare = new Compare<String>();
CompareTest test = new Comparetest();
test.testGeneric(compare);
//The inferred type Compare<String> is not a valid substitute for the bounded
//parameter <E extends Comparable<E>>
}
}
Solution
E extends the comparable < E > representation: type E can be compared with other objects of the same type
However, your comparison type does not meet the criteria It cannot be compared with another Compare < T > can only compare itself to t, not < T > because it is declared as
public class Compare<T> implements Comparable<T>
It's hard to understand what you want to achieve with this comparison type
