• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Generic methods and generic parameters

 
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


I know that Collection<T> is the return type but what does the <T> just after the keyword static do?

Thanks
 
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).
 
O. Ziggy
Ranch Hand
Posts: 430
Android VI Editor Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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>.

Many Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I think you've got it
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic