Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generic Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Generic Problem" Watch "Generic Problem" New topic
Author

Generic Problem

gaurav gupta sitm
Greenhorn

Joined: Jan 12, 2011
Posts: 22
what is the difference amongst <?> , <? extends Object> and <Object>
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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<?>.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


<?> 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.


SQL Query Interview Question
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3649
    
  17

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Generic Problem