• Post Reply Bookmark Topic Watch Topic
  • New Topic

Question on Generics  RSS feed

 
raja singh kumar
Ranch Hand
Posts: 189
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question on Generics:

What is the difference between ArrayList<T> and ArrayList<?>?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Difference is probably the wrong word to use here, and wildcards is a feature of generics.

Henry
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi raja,
Raja wrote:What is the difference between ArrayList<T> and ArrayList<?>

ArrayList<T> ---> here the type parameter is T i.e, you know that the List is containing an instances of type T only.here you can remove or add an instance of type T in it.
ArrayList<?> ---> here you are using a unbounded wildcard("?") as a type parameter.it means your arraylist is containing instances of UNKNOWN TYPE.Arraylist<?> is permitted to contain instances of only a single Type but compiler does not know about that single Type(that can be String,Object,Integer...etc).now due to this reason compiler does not allow a user to add something in it except null.suppose you are trying to add String in it but at the same time compiler will think that it can contain instances of Integer also so such operation could break the type safety and thus compiler will deny for it.

please have a look here for FAQ about generics for further details.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!