That makes it a generic method, which means that T is determined by the way the method is called.
You've probably seen generic classes, where an object is created with a supplied type - like Collection<String>? Well, this is similar, but it means that each call to the method can have a separate generic type. So you could use that method like this:
Ok so T just means that the method is a generic type.
In the following example:
Why does the compiler allow the call to the genericFromArrayToCollection() method only if the declared type of the collection is the parent of the declared type of the array. Why is this please?
When i uncomment the line marked #4 i get the following error
Because in the signature
they have to be the same T for each argument. So there needs to be a T that will satisfy that.
For #4, you've got an Object[] and a Collection<Animal>. There's no T that will work.
#2 is different, though. There you've got an Animal[] and a Collection<Object>. In this case, T can be Object. The difference is that an Animal[] IS-AN Object[], but a Collection<Animal> IS-NOT-A Collection<Object>. In other words:
The difference is because with generics the type is not available at runtime (type erasure), which means you need to be stricter to keep type safety. (And this is also why wildcards are needed in generics, whereas they aren't needed with arrays).
Thanks Mathew i think i got it. I think i didnt understand it before because i didnt know how T was supposed to be applied. I think what you are saying is that the object used in place of T needs to be compatible with every T defined in the 'template'.
In line 2, i am calling the method with Animal[],Collection<Object>. In this case T can be Object because Animal[] is an Object[] and Collection<Object> is a Collection<Object>
In line 4, i am calling the method with Object[], Collection<Animal>. In this case T cannot be Object because even though Object[] is Object[], Collection<Animal> is not Collection<Object>.