• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generic Problem

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the difference amongst <?> , <? extends Object> and <Object>
 
Saloon Keeper
Posts: 15615
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no difference between <?> and <? extends Object>, except that you can't cast a raw type to <? extends Object> without a warning (If I recall correctly).

<Object> is different though. If you have a List<Object>, you may only store actual Object instances in it, not subclasses, like String.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:<Object> is different though. If you have a List<Object>, you may only store actual Object instances in it, not subclasses, like String.


That's not right. You can store anything in them (otherwise you'd be breaking the substitution principle).

But you can't assign a List<String> to a List<Object> reference, whereas you can assign a List<String> to a List<?>.
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

<?> is commonly refereed as wild card and there are three in nature

<?> -- you can assign anything e.g List<String> or List<Integer>
<? extends Object > -- you can assign anything which is either Object of subclass of Object
<? super MyClass> -- you can assign anything which is either MyClass or super class of MyClass.
 
Stephan van Hulst
Saloon Keeper
Posts: 15615
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matthew Brown wrote:

Stephan van Hulst wrote:<Object> is different though. If you have a List<Object>, you may only store actual Object instances in it, not subclasses, like String.


That's not right. You can store anything in them (otherwise you'd be breaking the substitution principle).

But you can't assign a List<String> to a List<Object> reference, whereas you can assign a List<String> to a List<?>.


Oops! I'm sorry, I meant you can't store a reference to a List<String> in a List<Object> variable!
Sorry for the confusion.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic