-2

whats the difference between

public static <T> double sums(Collection<T extends Number> numbers) {} 

and this

public static <T extends Number> double sums(Collection<T> numbers) {}

and this

public static double sums(Collection<? extends Number> numbers) {}

and this

public static <T> double sums(Collection<? extends Number> numbers) {}

i get that we mention type parameter <T> before return type if we are using generics for only one method.and we can use the type parameter T here since its declared as classes type parameter(public class generics<T extends Integer >{}). and i dont quite understand the difference between them. especially first and last. im getting compile error on first statement, and no error on last statement. why i need to use ? wildcard at first statement ? and may i know how having static keyword affects this method declaration?

edit: Im not using the type inside method so type parameter <T> or <?> should be functionally same. im using only one parameterized type argument which according to this post should be same even if we use wildcards there but im getting compile error when i use type parameter .

Santhosh
  • 71
  • 1
  • 5
  • 1
    The last variant doesn't use `T` at all.....? – user202729 Aug 02 '18 at 05:06
  • Your first does not compile. – zhh Aug 02 '18 at 05:07
  • This will be helpful: https://docs.oracle.com/javase/tutorial/extra/generics/index.html Please try to find similar questions and other help from Google before asking questions, Happy coding!! – TapanHP Aug 02 '18 at 05:08
  • 1
    Possible duplicate of [Java generic method parameter type inside brackets vs. outside brackets](https://stackoverflow.com/questions/17448884/java-generic-method-parameter-type-inside-brackets-vs-outside-brackets) – Silvio Mayolo Aug 02 '18 at 05:08
  • https://stackoverflow.com/questions/18176594/when-to-use-generic-methods-and-when-to-use-wild-card – user202729 Aug 02 '18 at 05:08
  • @SilvioMayolo according to that question both has same functionality, but OP seems to have compile issues on first code. – amarnath harish Aug 02 '18 at 05:58

1 Answers1

2

Let's break them each down.

  1. This is nonsense. Don't do it. There's a reason it won't compile.

  2. Here, we're defining a generic function sums. Our function can be called with a collection containing elements of some specific numerical type.

  3. This is similar to (2). More on that below.

  4. This is a strange version of (3). Don't do this either; you've just declared a type parameter and not used it, so there's really no point.

Now, (2) and (3) are very similar. When we use ? as a type parameter, we're essentially saying "this can be whatever, but I don't particularly care what its value is". On the other hand, when we explicitly use T, we can use that type parameter to do other things. It's a type-level variable.

public static <T extends Number> double sums(Collection<T> numbers) {
  for (T arg : numbers) {
    // Do something with arg
  }
}

We can iterate over the structure, and our arg is well-typed since we know what sort of thing is in the collection. With (3), we lose some information.

public static double sums(Collection<? extends Number> numbers) {
  for (Number arg : numbers) {
    // Do something with arg
  }
}

We know numbers contains numbers of some specific type, but we don't know what it is, so we have to fall back to the more general but still safe Number.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • 1
    instead of saying nonsense code you could have said why its nonsense. and also the reason why it wont compile. – Santhosh Aug 02 '18 at 05:55
  • 1
    With 3 and 4, you cannot add anything to the collection, since the type of elements is specific but unknown. – MC Emperor Aug 02 '18 at 06:05
  • the question is not related to PECS.im simply asking about the syntactic usage of type parameter and wildcard. – Santhosh Aug 03 '18 at 04:56
  • @MCEmperor: "With 3 and 4, you cannot add anything to the collection" Well you *can* pass it to another method with signature #2, which *can* add stuff to it (although it can only add elements from the collection to itself, since it doesn't know what `T` is), so it can, indirectly, do anything #2 can. – newacct Aug 24 '18 at 22:25
  • @newacct Sure, in that way it's possible. And one can also simply typecast it. – MC Emperor Aug 25 '18 at 07:35