• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 14502
325
  • 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: 14502
325
  • 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.
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic