What is the difference in following 2 lines?
public static <T extends Comparable<? super T>> int methodX(List<T> data)
public static <T> int methodX(List<? extends Comparable<? super T>> data)
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Your first option is a “stricter” parametrisation. Meaning, you’re defining the class
Twith a bunch of restrictions, and then use it later on withList. In your second method, the parameter classTis generic with no conditions, and theLists class parameter is defined in terms of the parameterT.The second way is syntactically different as well, with a
?instead of the first option’sT, because in the parameter definition you aren’t defining the type parameterTbut rather using it, so the second method cannot be as specific.The practical difference that comes out of this is one of inheritance. Your first method needs to be a type that is comparable to a super class of itself, whereas the second type need only be comparable to an unconditional/unrelated
T:If you change the
ComparableofPersonto be able to compareObjectorPerson(the inheritance tree of the base class) thenmethodXwill also work.