• Post Reply Bookmark Topic Watch Topic
  • New Topic

generics and array?  RSS feed

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am looking at the interface of Collection<E> extends Iterable<E> (from the collection API)
and there is a method there that looks like this:



Could you please explain to me what <T> T[] means?

Thanks.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<T> means "this method uses a generic parameter, which we will call T". T represents a type - a class, or interface. We don't know much about it at this point, but hopefully it will be resolved by the time this method is called.

T[] means "this method returns an array of type T" (whatever T is).

Both of these are defined by their position in the declaration, at least as much as by their own syntax. Basically, in a method declaration, whatever occurs immediately before the method name (separated by whitespace) must be the return type of the method. In this case, it's T[]. As for the generic parameter <T>, I forget exactly when it needs to be declared, except it's somewhere before the return type. You can look it up if you care.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can read more about Generics here:
http://download.oracle.com/javase/tutorial/java/generics/index.html
http://download.oracle.com/javase/tutorial/extra/generics/index.html
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys. It's been very helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!