Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java generic: declaration of recursive types

Tags:

java

generics

In Effevtive Java I see a declaration: public static <T extends Comparable<T>> T max(List<T> list), type variable T is a type can be compared to itself. My question is what's the difference when I remove <T> from Comparable ? I mean this public static <T extends Comparable> T max(List<T> list) .

like image 235
user2018791 Avatar asked Sep 07 '26 13:09

user2018791


1 Answers

That means that you will then expect a type that is comparable to Object, since java infers generic types to be Object when not provided.

<T extends Comparable> // The generic type for Comparable is inferred as Object as it was not provided.

This means that the type provided to the list would need to implement compareTo(Object o) and implement Comparable (notice the missing generic type to Comparable), which I assume is not intended.

like image 182
SamTebbs33 Avatar answered Sep 10 '26 01:09

SamTebbs33



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!