File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes What is the difference between of using Collection<?> and Collection<Object> Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "What is the difference between of using Collection<?> and Collection<Object>" Watch "What is the difference between of using Collection<?> and Collection<Object>" New topic
Author

What is the difference between of using Collection<?> and Collection<Object>

santhosh.R gowda
Ranch Hand

Joined: Apr 06, 2009
Posts: 296
Dear All,

I want to know the difference between Collection<?> and Collection<Object>



they are telling
Object is not a super type of all kinds of collections


Please can you elaborate this statement because Object is super of all the kinds as i know


Creativity is nothing but Breaking Rules
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

You can use the Edit button () to edit your post. I have copy/pasted your second message into the first one this time.


[My Blog]
All roads lead to JavaRanch
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19759
    
  20

Collection<Object> is just that - a Collection with generic type Object. You can retrieve elements as Object references and put any object in it.
Collection<?> is a Collection with an unknown generic type. It could be Integer, String or anything else. Since the type is unknown elements can only be retrieved as references to the common super type -- Object. However, since you don't know the type you can't add anything but null. Anything else might lead to problems later on (e.g. if the actual generic type is Integer and you add a String -- oops!).

So that's the difference: with <?> you can't add anything, with <Object> you can.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is the difference between of using Collection<?> and Collection<Object>