File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 3598
    
  14

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: 4343
    
    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: 281


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


http://javarevisited.blogspot.com - java classpath - Java67 - java hashmap - java logging tips java interview questions Java Enum Tutorial
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3598
    
  14

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Generic Problem
 
Similar Threads
Pass by reference problem in EJB's
How many objects eligible for GC??
Garbage collection program really confusing me?
String
Question about Sequence Diagram