Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

16. Collections and Generics: Data struc... > Alternate syntax for doing the same ...

Alternate syntax for doing the same thing

You probably remember that when we looked at the sort() method, it used a generic type, but with an unusual format where the type parameter was declared before the return type. It’s just a different way of declaring the type parameter, but the results are the same:

This:

public <T extends Animal> void takeThing(ArrayList<T> list)

Does the same thing as this:

public void takeThing(ArrayList<? extends Animal> list)

There are no Dumb Questions

Q:

Q: If they both do the same thing, why would you use one over the other?

A:

A: It all depends on whether you want to use “T” somewhere else. For example, what if you want the method to have two arguments—both of which are lists of a type that extend Animal? In that case, it’s more efficient to just declare the type parameter once:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial