Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Question on Generics

 
Ranch Hand
Posts: 104
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I am preparing for IZO-809 certification and i am reading Kathy Sierra book. Its really very good and very informative. I have a question on mind and is as follows:

List<?> list = new ArrayList<Integer>();

I cannot add anything to the list , what is the use of the above statement ? Where can i use it and what are the use cases ?

Thanks & Regards,

Swapna
 
Saloon Keeper
Posts: 10407
223
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You wouldn't write a statement like that, because typically you don't want to throw away generic type information if you don't have to.

You declare a method parameter like that if you don't care about the exact type of the elements: you're only going to call methods that all objects support, such as toString():

You declare a local variable like that if you have to cast a non-generic supertype to a generic subtype. Here's an example from a custom list implementation:
 
Swapna latha
Ranch Hand
Posts: 104
jQuery Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You wouldn't write a statement like that, because typically you don't want to throw away generic type information if you don't have to.

You declare a method parameter like that if you don't care about the exact type of the elements: you're only going to call methods that all objects support, such as toString():

You declare a local variable like that if you have to cast a non-generic supertype to a generic subtype. Here's an example from a custom list implementation:



Thank you very much Stephan, but i haven't understood "You declare a local variable like that if you have to cast a non-generic supertype to a generic subtype. Here's an example from a custom list implementation".

So we use wildcards like ? only for reading right, but not for adding or deleting (if the passed argument is a list)

Thanks & Regards,

Swapna
 
Marshal
Posts: 65034
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can declare a variable or a parameter as List<?>, but the only thing you can add to it is null. Obviously you can pass a List from elsewhere, which has already been populated.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!