0
public <T extends String> void doStuff1(List<T> param) {
}

public void doStuff2(List<? extends String> param) { 
}

From my understanding, those two methods are identical. I am wondering which one is the correct way, why that is and if there is a scenario that "forces" me to use wildcards at all.

midorlo
  • 33
  • 5
  • 3
    Side note: `x extends String` doesn't make much sense since `String` can't be extended :) – Thomas Sep 29 '17 at 09:31
  • 1
    @MatthiasBurger not it is not only you and they _aren't_ identical. But the difference is not the return value as both return void, it's the parameter and what the compiler allows you to do with that (e.g. in `doStuff1` you'd be allowed to add elements of type `T` to the list, in `doStuff2` the compiler doesn't know the actual type of the elements and thus won't allow you to add to the list). – Thomas Sep 29 '17 at 09:33
  • 1
    @MatthiasBurger Both return types are void. The part in angle brackets in the first signature is a type parameter, not a return type. – Dawood ibn Kareem Sep 29 '17 at 09:35
  • The answer to this question is the third bullet point, near the bottom of Rohit Jain's excellent answer to the duplicate. – Dawood ibn Kareem Sep 29 '17 at 09:37
  • 1
    Indeed, C# is the work of the devil. – Dawood ibn Kareem Sep 29 '17 at 09:43

0 Answers0